4 ms·
Curiously, when I did transcribe the C++ code to C: #include <stdio.h> int main() { FILE* in = fopen("/tmp/numbers", "r"); for (int c, n = 0; (c =
by ncmncm 4y ago
Curiously, when I did transcribe the C++ code to C:
#include <stdio.h>
int main() {
FILE* in = fopen("/tmp/numbers", "r");
for (int c, n = 0; (c = getc(in)) != EOF;) {
if (c != '\n') {
n = n * 10 + (c - '0');
} else putchar((n & 7) + '0'), n = 0;
}
putchar('\n');
}
it takes 233 ms instead of the expected 84 ms.
So, go figure, again.
- jstimpfle 4y agoI don't know the context of why you guys are doing these microbenchmarks, but consider doing proper custom buffering for high speed number conversion. Or at least use fgetc_unlocked() / fputc_unlocked(). The normal FILE reading functions take a lock, which is quite slow for single-byte reads and writes.
- ncmncm 4y agoI guess that answers that question. I had forgotten that C got such a dumb change.
- jstimpfle 4y agoCan't see a good reason for the locking either, other than that printf is commonly used for debugging, which without locking would probably leave a lot of users puzzled with random crashes in multithreaded scenarios.