Why is this C code faster than this C++ code ? getting biggest line in file

My guess is that it is a problem with the compiler options you are using, the compiler itself, or the file system. I just now compiled both versions (with optimizations on) and ran them against a 92,000 line text file:

c++ version:  113 ms
c version:    179 ms

And I suspect that the reason that the C++ version is faster is because fgetc is most likely slower. fgetc does use buffered I/O, but it is making a function call to retrieve every character. I’ve tested it before and fgetc is not as fast as making a call to read the entire line in one call (e.g., compared to fgets).

Leave a Comment