c - learning sample of likely() and unlikely() compiler hints -
how can demonstrate students usability of likely
, unlikely
compiler hints (__builtin_expect
)?
can write sample code, several times faster these hints comparing code without hints.
here 1 use, inefficient implementation of fibonacci numbers:
#include <stdio.h> #include <inttypes.h> #include <time.h> #include <assert.h> #define likely(x) __builtin_expect((x),1) #define unlikely(x) __builtin_expect((x),0) uint64_t fib(uint64_t n) { if (opt(n == 0 || n == 1)) { return n; } else { return fib(n - 2) + fib(n - 1); } } int main(int argc, char **argv) { int i, max = 45; clock_t tm; if (argc == 2) { max = atoi(argv[1]); assert(max > 0); } else { assert(argc == 1); } tm = -clock(); (i = 0; <= max; ++i) printf("fib(%d) = %" priu64 "\n", i, fib(i)); tm += clock(); printf("time elapsed: %.3fs\n", (double)tm / clocks_per_sec); return 0; }
to demonstrate, using gcc:
~% gcc -o2 -dopt= -o test-nrm test.c ~% ./test-nrm ... fib(45) = 1134903170 time elapsed: 34.290s ~% gcc -o2 -dopt=unlikely -o test-opt test.c ~% ./test-opt ... fib(45) = 1134903170 time elapsed: 33.530s
a few hundred milliseconds less. gain due programmer-aided branch prediction.
but now, programmer should doing instead:
~% gcc -o2 -dopt= -fprofile-generate -o test.prof test.c ~% ./test.prof ... fib(45) = 1134903170 time elapsed: 77.530s /this run slowed down profile generation. ~% gcc -o2 -dopt= -fprofile-use -o test.good test.c ~% ./test.good fib(45) = 1134903170 time elapsed: 17.760s
with compiler-aided runtime profiling, managed reduce original 34.290s 17.760s. better programmer-aided branch prediction!
Comments
Post a Comment