Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 7th, 2004, 07:03 AM
Registered User
 
Join Date: May 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Buffered vs Unbuffered Reading

I have the following lines of code. The first chunk does a buffered reading using a 4Kb byte buffer. The other chunk does a sequential read.

When i measure the time taken by the two chunks of code i find out that unbuffered reading code takes less time then buffered reading code. While actually buffered should take less time.

Can any body tell me what i am doing wrong here.


//Buffered Read
string fileName="c:\\file";//open a 256 MB file
fileStream=new FileStream(fileName,FileMode.Open);
byte []readBuffer=new byte[4096];

while(fileStream.Read(readBuffer,0,readBuffer.Leng th)!=0)
{}



//Unbuffered Read
string fileName="c:\\file";//open a 256 MB file
fileStream=new FileStream(fileName,FileMode.Open);

int temp;
while((temp=fileStream.ReadByte())!=-1)
{}
 
Old May 7th, 2004, 10:40 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Why should buffered reading take less time?

If you were using two threads- one reading from the drive and creating the buffer while the other does the work on the buffer, then you could see some time improvement. However, in this case your reading to the buffer and then using the buffer after the buffer is loaded. Your not getting any performance gain- and in fact your taking a hit by using the buffer - because your loading it.

The value of a buffer is when you have a read source that is slower than the proccess source.. like a buffer for internet streaming.


Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old May 8th, 2004, 02:22 AM
Registered User
 
Join Date: May 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

My intial apprehnsion was also what you said. Because of the fact that the byte array is getting populated the code is taking more time.

Can u suggest a method which does buffered reading without any performance overheads.

I am just trying to get Buffered and Unbuffered read rates. Kind of a harddisk bench mark.





Similar Threads
Thread Thread Starter Forum Replies Last Post
reading data darkhalf XSLT 1 September 7th, 2006 02:51 PM
Reading XML evidica C# 0 July 25th, 2006 10:33 AM
reading emails santosh82 VB.NET 2002/2003 Basics 0 May 24th, 2006 07:59 PM
arabic reading i_m_el ASP.NET 1.0 and 1.1 Basics 0 February 27th, 2006 04:03 AM
reading table darkhalf Javascript 1 October 6th, 2005 11:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.