472,145 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 3990
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Fuzzyman | last post: by
5 posts views Thread by Rich | last post: by
reply views Thread by leo001 | last post: by

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.