473,507 Members | 2,416 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_output_filename = "unbuffered_output.txt";
const char *buffered_output_filename = "buffered_output.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_size, file_stat.st_blksize);
}

/* 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_size * sizeof (char));

/* Read in data. */
retval = (int)fread ((void *)io, 1, file_stat.st_size, fd_in);
if (retval != file_stat.st_size && 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_output_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(unbuffered): %u sec %u microsec.\n",
begin_val.tv_sec, begin_val.tv_usec);

/* Write 10 times for unbuffered test. */
for (i = 0; i < 10; ++i) {
retval = (int)fwrite ((void *)io, 1, file_stat.st_size, fd_out);
if (retval != file_stat.st_size && 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(unbuffered): %u sec %u microsec.\n",
finish_val.tv_sec, finish_val.tv_usec);
begin_val.tv_sec = finish_val.tv_sec - begin_val.tv_sec;
begin_val.tv_usec = finish_val.tv_usec - begin_val.tv_usec;
fprintf (stdout, "Time spent on unbuffered test: %u sec %u microsec.\n",
begin_val.tv_sec, begin_val.tv_usec);

fclose (fd_out);
} else {
/* Open file for buffered output. */
fd_out = fopen (buffered_output_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_sec, begin_val.tv_usec);

/* Write 10 times for buffered test. */
for (i = 0; i < 10; ++i) {
retval = (int)fwrite ((const void *)io, 1, file_stat.st_size, fd_out);
if (retval != file_stat.st_size && 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_sec, finish_val.tv_usec);
begin_val.tv_sec = finish_val.tv_sec - begin_val.tv_sec;
begin_val.tv_usec = finish_val.tv_usec - begin_val.tv_usec;
fprintf (stdout, "Time spent on buffered test: %u sec %u microsec.\n",
begin_val.tv_sec, begin_val.tv_usec);

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(unbuffered): 1134782571 sec 685364 microsec.
Time finish(unbuffered): 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 4151
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.linux.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.learn.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.com, 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_Keith) 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
1154
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 : ...
5
5136
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...
1
2869
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
7674
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...
3
2435
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...
7
7147
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
2222
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...
6
10793
by: zeeshan708 | last post by:
what is the difference between unbuffered and buffered stream??
8
14704
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...
0
7223
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,...
0
7319
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,...
1
7031
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...
1
5042
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...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.