Why fgetc too slow?

Posted by user14284 on Programmers See other posts from Programmers or by user14284
Published on 2012-11-04T17:42:32Z Indexed on 2012/11/04 23:16 UTC
Read the original article Hit count: 122

Filed under:

I've written a program that reads an whole file via fgetc:

while ((c = fgetc(f)) != EOF) { ... }

But the program is too slow. When I changed fgetc to fread,

static unsigned char buf[4096];

while ((n = fread(buf, 1, sizeof(buf), f)) > 0) { ... }

the program works about 10 times faster.

Why? As I know, fgetc is a buffered function, so it should work as fast as the second version with explicit buffer, isn't it?

© Programmers or respective owner

Related posts about c