StokeBloke.com

dont use stdio getc() or fgetc(), they are slow

Well I thought I would write about an issue I have known about for some time, but it appears others still dont.

Its regarding stdio fgetc() performance. Mainly that using fgetc() is always slow and should rarely be used.

Here is a comparison of fgetc(), fread() and fgets() when reading a 333Mb mbox file created via thunderbird.

operation time
fgetc() 7.15 seconds
fgets() 0.65 second
fread() 0.08 seconds

Times are take on a quad core 2.4Ghz (linux 2.6.2.4 tuxonice r4 gentoo)

The performance issues clear show that retrieving information in larger ‘chunks’ is more efficient and its scary how different the performance is. Unless you only want 1 or a few characters, you should probably not use getc()

The code

fgetc()

clock_t start;
clock_t end;
FILE* testFile;
char buf[8*1024];
unsigned char chr;

testFile = fopen("testdata", "r");
start = clock();
    
while(!feof(testFile)) {
    chr = (unsigned char)getc(testFile);
}

end = clock();
printf("fgetc() %f seconds\n", ((float)end - (float)start) / (float)CLOCKS_PER_SEC);

fgets()

clock_t start;
clock_t end;
FILE* testFile;
char buf[8*1024];

testFile = fopen("testdata", "r"); 
start = clock();

do {}  while (fgets (buf, sizeof(buf), testFile) != NULL);

end = clock();
printf("fgets() %f seconds\n", ((float)end - (float)start) / (float)CLOCKS_PER_SEC);

fread()

clock_t start;
clock_t end;
FILE* testFile;
char data[8*1024];
int r = 1;

testFile = fopen("testdata", "r");
start = clock();
    
while(r!=0) {
    r = fread(data, sizeof(char), 1024*8, testFile);
}
    
end = clock();
printf("fread() %f seconds\n", ((float)end - (float)start) / (float)CLOCKS_PER_SEC);

One Response to “dont use stdio getc() or fgetc(), they are slow”

  1. Count the line Says:

    […] it may execute a bit of code to test something that's never changes. I already found this page: http://www.stokebloke.com/wordpress/…they-are-slow/ which suggests the library function 'getc' should not be used, but fread is OK. For someone using […]

Leave a Reply