ReadByte length

C

Carmelo

Hi there,

I'm a newbie on C# and I was trying just to open a file, read it
byte-a-byte and close controlling the length of this operation. Doing
that with a 500Kb file I get a duration of about 3,5 seconds and doing
it on a 8Mb file i get 55 seconds!!. Is that ok??, or is something
wrong...I guess that is too much time, I expect having a lower times,
but don't know. See code:

DateTime start = DateTime.Now;

string fileName = args[0];
FileInfo file = new FileInfo( fileName );
Stream buff = new BufferedStream( new FileStream( file.FullName,
FileMode.Open,
FileAccess.Read ));
BinaryReader ins = new BinaryReader(buff);

while ( (ins.BaseStream.Position) < ins.BaseStream.Length )
{
ins.ReadByte();
}
buff.Close();
TimeSpan total = DateTime.Now.Subtract( start );
Console.WriteLine( "time length=
"+total.TotalMilliseconds.ToString());
 
J

Jon Skeet [C# MVP]

Carmelo said:
I'm a newbie on C# and I was trying just to open a file, read it
byte-a-byte and close controlling the length of this operation. Doing
that with a 500Kb file I get a duration of about 3,5 seconds and doing
it on a 8Mb file i get 55 seconds!!. Is that ok??, or is something
wrong...I guess that is too much time, I expect having a lower times,
but don't know. See code:

The problem is precisely because you *are* reading it byte by byte.
Instead, read it in chunks, using Read (byte[], int, int) on the
stream, remembering to use the return value appropriately. Note that
with this usage there's no need to wrap a BufferedStream round the
FileStream. You should also use a using statement to make sure the
stream is disposed of even if an exception is thrown.
 
W

Willy Denoyette [MVP]

I do agree with Jon that you don't have to wrap the FileStream.
But I don't agree that your problem is related to the fact you are reading
byte by byte.

The problem is the : ins.BaseStream.Length in the while loop.

while ( (ins.BaseStream.Position) < ins.BaseStream.Length )

By doing so you are asking the FileSystem for the length of the file at each
file pointer change (per byte in your case).
Change your code like this:
long len = ins.BaseStream.Length;
while ( (ins.BaseStream.Position) < len)
.....
And watch the difference :)

Willy.
 
J

Jon Skeet [C# MVP]

Willy Denoyette said:
I do agree with Jon that you don't have to wrap the FileStream.
But I don't agree that your problem is related to the fact you are reading
byte by byte.

The problem is the : ins.BaseStream.Length in the while loop.

while ( (ins.BaseStream.Position) < ins.BaseStream.Length )

By doing so you are asking the FileSystem for the length of the file at each
file pointer change (per byte in your case).

Ah yes, hadn't noticed that. That is indeed where the bulk of the time
is going. However, reading in a chunk at a time still makes a
performance difference which is worth taking advantage of, IMO.
Change your code like this:
long len = ins.BaseStream.Length;
while ( (ins.BaseStream.Position) < len)
....
And watch the difference :)

Or preferably, don't use that at all - just test the return value each
time instead. That way you'll read to the end of the file even if it
changes while you're reading it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top