Then we compile with -O3:#include <stdio.h>
unsigned long int fib(int n) {
if (n < 3)
return 1;
return fib(n-2) + fib(n-1);
}
int main() {
int i;
for (i=1; i < 46; i++)
printf("fib(%d) = %ld\n", i, fib(i));
return 0;
}
llvm-gcc -O3 fib.c -o fib-llvm-gcc
gcc -O3 fib.c -o fib-gcc
And the results:
time ./fib-llvm-gcc
real 0m3.053s
user 0m3.052s
sys 0m0.000s
time ./fib-gcc
real 0m11.176s
user 0m11.173s
sys 0m0.004s
That is something.
What comes to code size (both in x86 assembly), the faster is also the smaller:
wc fib-llvm-gcc.S
108 288 1900 fib-llvm-gcc.S
wc fib-gcc.S
291 738 4485 fib-gcc.S
1 comment:
You should compare a less bad implementation of fib(int), such as
unsigned long int fib(int n) {
unsigned long int f1=1;
unsigned long int f2=1;
for (int i=n-2;i>0;i--){
unsigned long int t=f1+f2;
f2=f1;
f1=t;
}
return f1;
}
Post a Comment