Skip to content

Instantly share code, notes, and snippets.

@dkorolev
Created May 21, 2018 08:26
Show Gist options
  • Save dkorolev/b36ab9c6ca823e540b1a37d171e87cf5 to your computer and use it in GitHub Desktop.
Save dkorolev/b36ab9c6ca823e540b1a37d171e87cf5 to your computer and use it in GitHub Desktop.
fgets() vs. gets() timing.
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ cat step5_rides_by_months_unoptimized.c
// To run: gcc -O3 step5_rides_by_months_unoptimized.c && time ./a.out | tee >(md5sum)
//
// Count rides by month, print the counters in the lexicographically sorted order of keys.
// The "canonical C" implementation, with no extra checks, unsafe in a few ways, but still only 2x slower than `wc -l`.
#include <stdio.h>
int MM[12];
int main(int argc, char** argv) {
char line[10000]; // Unsafe if we encounter longer lines. -- D.K.
FILE* f = fopen(argc >= 2 ? argv[1] : "../cooked.csv", "r");
while (fgets(line, sizeof(line), f)) {
++MM[(line[7] - '0') * 10 + (line[8] - '0') - 1]; // Unsafe if those two raw characters are not "01" .. "12" range.
}
for (int m = 0; m < 12; ++m) {
printf("%d %02d/%d\n", MM[m], m + 1, 2016);
}
fclose(f);
}
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ diff step5_rides_by_months_unoptimized.c step5a_rides_by_months_unoptimized.c
1c1
< // To run: gcc -O3 step5_rides_by_months_unoptimized.c && time ./a.out | tee >(md5sum)
---
> // To run: gcc -O3 step5a_rides_by_months_unoptimized.c && time cat ../cooked.csv | ./a.out | tee >(md5sum)
10c10
< int main(int argc, char** argv) {
---
> int main() {
12,13c12
< FILE* f = fopen(argc >= 2 ? argv[1] : "../cooked.csv", "r");
< while (fgets(line, sizeof(line), f)) {
---
> while (gets(line)) {
19d17
< fclose(f);
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ gcc -O3 step5_rides_by_months_unoptimized.c
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ time cat ../cooked.csv | ./a.out | md5sum
bd5b6d7f389e64aab50c56fed61eaded -
real 0m1.002s
user 0m0.589s
sys 0m0.417s
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ time cat ../cooked.csv | ./a.out | md5sum
bd5b6d7f389e64aab50c56fed61eaded -
real 0m0.874s
user 0m0.487s
sys 0m0.389s
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ time cat ../cooked.csv | ./a.out | md5sum
bd5b6d7f389e64aab50c56fed61eaded -
real 0m0.877s
user 0m0.431s
sys 0m0.448s
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ gcc -O3 step5a_rides_by_months_unoptimized.c
step5a_rides_by_months_unoptimized.c: In function ‘main’:
step5a_rides_by_months_unoptimized.c:12:10: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
while (gets(line)) {
^~~~
fgets
/tmp/cc7WVDJk.o: In function `main':
step5a_rides_by_months_unoptimized.c:(.text.startup+0x51): warning: the `gets' function is dangerous and should not be used.
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ time cat ../cooked.csv | ./a.out | md5sum
bd5b6d7f389e64aab50c56fed61eaded -
real 0m1.430s
user 0m0.584s
sys 0m1.957s
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ time cat ../cooked.csv | ./a.out | md5sum
bd5b6d7f389e64aab50c56fed61eaded -
real 0m1.392s
user 0m0.614s
sys 0m1.853s
dima@dima-x1:~/github/dkorolev/current/examples/datafest_talk/tier2_rides_by_months@unstable $ time cat ../cooked.csv | ./a.out | md5sum
bd5b6d7f389e64aab50c56fed61eaded -
real 0m1.502s
user 0m0.615s
sys 0m2.049s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment