3 ms·
Yet, still slower than the C++ code. But I bet just transcribing the C++ code to the most naïve C code would go as fast as the C++.
by ncmncm 4y ago
Yet, still slower than the C++ code.
But I bet just transcribing the C++ code to the most naïve C code would go as fast as the C++.
- marginalia_nu 4y agoI get these times for your C++ real 0m0.854s user 0m0.564s sys 0m0.054s
- ncmncm 4y agoI am corrected.
- ncmncm 4y agoCuriously, 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.