The standard output is normally flushed when you write a newline. If you want to test this properly, open a file and write to it. For your tests to be useful, you will have to write a lot more data than just a few integers. You should find that omitting fflush
will result in significantly faster code. Try timing these two loops...
With flushing:
FILE * fp = fopen("scratch", "w");
for( int i = 0; i < 1000000; i++ ) {
fprintf( fp, "Hello world" );
fflush(fp);
}
fclose(fp);
Without flushing:
FILE * fp = fopen("scratch", "w");
for( int i = 0; i < 1000000; i++ ) {
fprintf( fp, "Hello world" );
}
fclose(fp);
On my machine, the results are:
With fflush: 4.57 seconds
Without fflush: 0.24 seconds