473,379 Members | 1,252 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

ReadByte length

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());
Nov 16 '05 #1
3 5677
Carmelo <ca*********@yahoo.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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.

"Carmelo" <ca*********@yahoo.com> wrote in message
news:78**************************@posting.google.c om...
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());

Nov 16 '05 #3
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul | last post by:
Hi, (First apologies if this is not the most relevant place to post this but I wasn't sure of where was and I am writing my app in VB.) I'm attempting to parse a binary file for which I have...
5
by: Mitchua | last post by:
Simplified a bit, I'm parsing HTML documents to get sentences e.g. my $html = get($URL); # remove all HTML TAGs...blah blah blah @sentences = split(/\./, $html)); then I'm trying to determine the...
14
by: Luiz Antonio Gomes Pican?o | last post by:
How i can store a variable length data in file ? I want to do it using pure C, without existing databases. I'm thinking to use pages to store data. Anyone has idea for the file format ? I...
4
by: randomtalk | last post by:
hello, I'm doing an exercise in "The C Programming Language, Second Edition". I just can't get one of the first chapter problem correct (I do realize however that there are answer exist on the...
1
by: Sathyaish | last post by:
I have the following scenario: Algorithm: 3DES Cipher Mode: CBC Key Size: 128-bit Block Size: 64 bit IV: 0x0000000000000000 (an eight byte array of zeros) The results I get using .NET with...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
14
by: Richard Maher | last post by:
Hi, Can anyone please show me a regular expression (or function or other) for padding out a string to a fixed number of bytes? At the moment I've got a var initialized to N spaces, where 'N'...
5
by: prads | last post by:
Hello, The pgm below opens a file, reads it and displays the contents and its length. However it displays a length of (length+2) and also the samplesread as (samplesread +2), where samplesread is...
0
by: LukasMalik | last post by:
Hi all, I have 2 different read methods: 1) public void Read() { while(true) { oSerialPort.Read(bytesToReadArray, 0, bytesToReadArray.Lenght);
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.