473,842 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is there no difference between buffered and unbuffered file IO?

hi everyone,

I have a program here to test the file IO(actually output) with buffer
turned on and off. What I want to see is that there will be obvious
differece in time. Here I have an input file scales 1.1M, what I did is
just copy it to another output file(ten times). But I don't think I see
any difference. My file system is ext3, with debian
sarge(kernel-2.6.8).
Here is the main part of my program:

/*
* This is a function to handle error.
* Not exactly handle, recently, it
* just print the error number for
* further handling.
*/
void
err_out (int err);

int
main (int argc, char **argv)
{
FILE *fd_in;
FILE *fd_out;
char *io;
struct stat file_stat;
int retval;
extern int errno;
struct timeval begin_val;
struct timezone begin_zone;
struct timeval finish_val;
struct timezone finish_zone;
unsigned short i;
const char *unbuffered_out put_filename = "unbuffered_out put.txt";
const char *buffered_outpu t_filename = "buffered_outpu t.txt";
char *buffer;

/* Get the size of the input file and then allocate space for input string. */
retval = stat ("./input.txt", &file_stat);
if (retval == -1) {
fprintf (stderr, "Error while getting the status of input file!\n");
err_out (errno);
exit (1);
} else {
fprintf (stdout, "Size of the input file: %d bytes.\nBuffer size of the file: %d.\n",
file_stat.st_si ze, file_stat.st_bl ksize);
}

/* Open the input file for input. */
fd_in = fopen ("./input.txt", "r");
if (fd_in == NULL) {
fprintf (stderr, "Error while openning input file for input!\n");
err_out (errno);
exit (2);
}
io = (char *)malloc (file_stat.st_s ize * sizeof (char));

/* Read in data. */
retval = (int)fread ((void *)io, 1, file_stat.st_si ze, fd_in);
if (retval != file_stat.st_si ze && ferror (fd_in)) {
fprintf (stderr, "Error while reading file for test!\n");
free (io);
exit (4);
}
fclose (fd_in);

fprintf (stdout, "Choose:\n1 . Unbuffered test.\n2. Buffered test.\n");
if ((char)getc (stdin) == 'u') {
/* Open file for unbuffered output. */
fd_out = fopen (unbuffered_out put_filename, "w");
if (fd_out == NULL) {
fprintf (stderr , "Error while openning file for unbuffered output!\n");
err_out (errno);
free (io);
exit (3);
}

/* Shutdown the buffer for unbuffered test. */
setvbuf (fd_out, NULL, _IONBF, 0);

/* Time counting for unbuffered test begins. */
retval = gettimeofday (&begin_val, &begin_zone) ;
if (retval == -1) {
fprintf (stderr, "Error while getting begin time!\n");
err_out (errno);
free (io);
exit (4);
}
fprintf (stdout, "Time begin(unbuffere d): %u sec %u microsec.\n",
begin_val.tv_se c, begin_val.tv_us ec);

/* Write 10 times for unbuffered test. */
for (i = 0; i < 10; ++i) {
retval = (int)fwrite ((void *)io, 1, file_stat.st_si ze, fd_out);
if (retval != file_stat.st_si ze && ferror (fd_out)) {
fprintf (stderr, "Error while writing file for unbuffered test!\n");
err_out (errno);
free (io);
exit (5);
}
}

/* Time counting for unbuffered test ends. */
retval = gettimeofday (&finish_val, &finish_zone );
if (retval == -1) {
fprintf (stderr, "Error while getting finish time!\n");
err_out (errno);
free (io);
exit (6);
}
fprintf (stdout, "Time finish(unbuffer ed): %u sec %u microsec.\n",
finish_val.tv_s ec, finish_val.tv_u sec);
begin_val.tv_se c = finish_val.tv_s ec - begin_val.tv_se c;
begin_val.tv_us ec = finish_val.tv_u sec - begin_val.tv_us ec;
fprintf (stdout, "Time spent on unbuffered test: %u sec %u microsec.\n",
begin_val.tv_se c, begin_val.tv_us ec);

fclose (fd_out);
} else {
/* Open file for buffered output. */
fd_out = fopen (buffered_outpu t_filename, "w");
if (fd_out == NULL) {
fprintf (stderr, "Error while openning file for buffered output!\n");
err_out (errno);
free (io);
exit (7);
}

/* Turn on the buffer for buffered test. */
buffer = (char *)malloc (131072 * sizeof (char));
setvbuf (fd_out, buffer, _IOFBF, 131072);

/* Time counting for buffered test begins. */
retval = gettimeofday (&begin_val, &begin_zone) ;
if (retval == -1) {
fprintf (stderr, "Error while getting begin time!\n");
err_out (errno);
free (io);
exit (8);
}
fprintf (stdout, "Time begin(buffered) : %u sec %u microsec.\n",
begin_val.tv_se c, begin_val.tv_us ec);

/* Write 10 times for buffered test. */
for (i = 0; i < 10; ++i) {
retval = (int)fwrite ((const void *)io, 1, file_stat.st_si ze, fd_out);
if (retval != file_stat.st_si ze && ferror (fd_out)) {
fprintf (stderr, "Error while writing file for buffered test!\n");
err_out (errno);
free (io);
free (buffer);
exit (9);
}
}

/* Time counting for buffered test ends. */
retval = gettimeofday (&finish_val, &finish_zone );
if (retval == -1) {
fprintf (stderr, "Error while getting finish time!\n");
err_out (errno);
free (io);
free (buffer);
exit (10);
}
fprintf (stdout, "Time finish(buffered ): %u sec %u microsec.\n",
finish_val.tv_s ec, finish_val.tv_u sec);
begin_val.tv_se c = finish_val.tv_s ec - begin_val.tv_se c;
begin_val.tv_us ec = finish_val.tv_u sec - begin_val.tv_us ec;
fprintf (stdout, "Time spent on buffered test: %u sec %u microsec.\n",
begin_val.tv_se c, begin_val.tv_us ec);

free (buffer);
fclose (fd_out);
}

free (io);

return 0;
}


The result is here:
Size of the input file: 1128838 bytes.
Buffer size of the file: 4096.
Choose:
1. Unbuffered test.
2. Buffered test.
b
Time begin(buffered) : 1134782563 sec 803489 microsec.
Time finish(buffered ): 1134782563 sec 852111 microsec.
Time spent on buffered test: 0 sec 48622 microsec.
Size of the input file: 1128838 bytes.
Buffer size of the file: 4096.
Choose:
1. Unbuffered test.
2. Buffered test.
u
Time begin(unbuffere d): 1134782571 sec 685364 microsec.
Time finish(unbuffer ed): 1134782571 sec 732220 microsec.
Time spent on buffered test: 0 sec 48622 microsec.

can anyone help me?

I've also tried to run this program on FreeBSD-5.4, but no diff either
Thanx!!!
pank7.yardbird

Dec 17 '05 #1
4 4202
On 16 Dec 2005 21:22:51 -0800, "pank7" <pa***********@ gmail.com> wrote
in comp.lang.c:
hi everyone,

I have a program here to test the file IO(actually output) with buffer
turned on and off. What I want to see is that there will be obvious
differece in time. Here I have an input file scales 1.1M, what I did is
just copy it to another output file(ten times). But I don't think I see
any difference. My file system is ext3, with debian
sarge(kernel-2.6.8).
Here is the main part of my program:


[snip code]

The C standard does not specify the relative speed of anything.

There are a large number of factors that can impact the result of a
test like this. These include the compiler, its library
implementation, the operating system, and its configuration, just to
name a few.

The results you obtained are just that, the results you obtained, and
the C language is indifferent as to whether they are what you
expected.

If you want more information, post in a platform specific group like
one of news:comp.os.li nux.development .* family. This is not a
language issue.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 17 '05 #2
Thank you! I appreciate that.

But could you please give me some suggestions on
how to do such a test? What shall I do if I wanna
see the differences?

Dec 17 '05 #3
On 17 Dec 2005 07:21:39 -0800, in comp.lang.c , "pank7"
<pa***********@ gmail.com> wrote:
Thank you! I appreciate that.
Please read this:
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
But could you please give me some suggestions on
how to do such a test? What shall I do if I wanna
see the differences?


The ways to test this are outside the scope of C, you';d have to ask
in groups specialising in your operating system, compiler and /or
hardware.
----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 17 '05 #4
"pank7" <pa***********@ gmail.com> writes:
Thank you! I appreciate that.

But could you please give me some suggestions on
how to do such a test? What shall I do if I wanna
see the differences?


We don't know what you're talking about.

Read <http://cfaj.freeshell. org/google/> to find out why.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 17 '05 #5

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

Similar topics

0
1169
by: Fuzzyman | last post by:
I use a simple python script to monitor downloads from my website. http://www.voidspace.org.uk/python/cgi.shtml#downman It serves the file in a loop using our old friend : ``sys.stdout.write(chunk)`` (After sending the relevant headers with filename and filesize of course).
5
5170
by: Rich | last post by:
Does anyone know the correct way of opening an unbuffered output file using STL? I attempted: ofstream myfile("fname.txt", ios::binary); myfile.setbuf(NULL, 0); and I was informed that setbuf is not a member of
1
2891
by: | last post by:
Is this even possible? I've found some references to specific "unbuffered" type methods that exist in older incarnations of basic_streambuf but not in newer ones. Info please. :P
23
7762
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard buffer ? are they same ? thanks ^^
3
2485
by: Brad | last post by:
I have a response filter which injects "standard" html into my pages. The filter works fine when the initial stream is small enough not to buffer...or....if I have a large unbuffered stream (i.e. I set buffer=false on a large page). Now the problem: If I turn on buffering on a large page, the page output (to the browser) is correct a few times (sometines just once, sometime 2-3 times...on the same page) then I seem to either lose data...
7
7176
by: Cell | last post by:
when both are connected to screen and the anything written to these two constant file pointers will go onto the screen ?
1
2272
by: LiorO | last post by:
Hi, I need to implement a high performance file copying. The File.Copy method in .NET is fast enough however I need to control the speed since it should be performed in the background without affecting the process too much. I've implemented a buffered (FileStream) copy using a queue for the bufferes. The performance is about 60% slower than the File.Copy. I've heard something about unbuffered stream, however I couldn't find anything...
6
10860
by: zeeshan708 | last post by:
what is the difference between unbuffered and buffered stream??
8
14757
by: zeeshan708 | last post by:
what is the difference between the buffered and unbuffered stream ??(e.g we say that cout is buffered and cerr is unbuffered stream) does that mean that the output sent to buffered stream have to go through a buffer (i.e a temporary storage registers) ,, if so then what is the big deal in unbuffered stream??
0
9871
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10944
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10610
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7855
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7035
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5884
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4089
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.