c - Is there any difference without fflush in that code? -
in cpp reference, claims fflush is:
causes output file stream synchronized actual contents of file.
indeed, don't understand means. wonder, in code, if take out fflush, there difference?
i tested it, seems there little bit difference, can't find pattern. 1 explain in details me? in advance.
#include<stdio.h> int i; int main() { fork(); for(i=0;i<1000;i++) { printf("%d\n",i); fflush(stdout);// without fflush, there difference? } }
the standard output flushed when write newline. if want test properly, open file , write it. tests useful, have write lot more data few integers. should find omitting fflush result in faster code. try timing these 2 loops...
with flushing:
file * fp = fopen("scratch", "w"); for( int = 0; < 1000000; i++ ) { fprintf( fp, "hello world" ); fflush(fp); } fclose(fp); without flushing:
file * fp = fopen("scratch", "w"); for( int = 0; < 1000000; i++ ) { fprintf( fp, "hello world" ); } fclose(fp); on machine, results are:
with fflush: 4.57 seconds without fflush: 0.24 seconds
Comments
Post a Comment