This example of why the right algorithm matters comes directly from my textbook. Here's the C implementation:
Bad:
#include <stdio.h>
#include <stdlib.h>
long int fib(long int n) {
if (n==0)
return 1;
if (n==1)
return 1;
return fib(n-1) + fib(n-2);
}
int main(int argc, char *argv[]) {
if (argc <= 1) {
fprintf(stderr, "argument?\n\n");
exit(1);
}
long int n = atol(argv[1]);
printf("f(%li) = %li\n",n,fib(n));
return 0;
}
Good:
#include <stdio.h>
#include <stdlib.h>
long int fib(long int n) {
long a=1, b=1, c;
int i;
for (i = 1;i < n; i++){
c = a + b; a = b; b = c;
}
return b;
}
int main(int argc, char *argv[]) {
if (argc <= 1) {
fprintf(stderr, "argument?\n\n");
exit(1);
}
long int n = atol(argv[1]);
printf("f(%li) = %li\n",n,fib(n));
return 0;
}
Output:
$ time ./fib2 38 ; time ./fib1 38 f(38) = 63245986 real 0m0.002s user 0m0.000s sys 0m0.004s f(38) = 63245986 real 0m1.492s user 0m1.428s sys 0m0.004s