Created
September 28, 2012 06:35
-
-
Save fanzeyi/3798284 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
inline int fibonacci(n, x1, x2) { | |
if (n == 0) { | |
return x1; | |
} | |
return fibonacci(n - 1, x2, x1 + x2); | |
} | |
int main() { | |
int i = 0; | |
for(i = 0; i < 99999999; i++) { | |
fibonacci(40, 0, 1); | |
} | |
return 0; | |
} | |
========================================== | |
$ gcc a.c && time ./a.out | |
./a.out 18.67s user 0.10s system 99% cpu 18.808 total | |
$ gcc a.c -O1 && time ./a.out | |
./a.out 5.79s user 0.02s system 99% cpu 5.813 total | |
$ gcc a.c -O2 && time ./a.out | |
./a.out 0.00s user 0.00s system 0% cpu 0.001 total | |
$ gcc a.c -O3 && time ./a.out | |
./a.out 0.00s user 0.00s system 0% cpu 0.001 total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func Fibonacci(n,x,y int) int { | |
if(n == 0) { | |
return x | |
} | |
return Fibonacci(n - 1, y, x + y) | |
} | |
func main() { | |
for i := 0; i < 99999999; i++ { | |
Fibonacci(40, 0, 1) | |
} | |
} | |
========================== | |
$ go build test.go && time ./test | |
./a 17.27s user 0.11s system 99% cpu 17.410 total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class test { | |
public static int fib(int n, int x, int y) { | |
if (n < 0) { | |
return x; | |
} else { | |
return fib(n - 1, y, x+y); | |
} | |
} | |
public static void main(String[] args) { | |
for(int i = 0; i < 99999999; i++) { | |
fib(40,0,1); | |
} | |
} | |
} | |
============================== | |
$ javac test.java | |
$ time java test | |
java test 9.06s user 0.01s system 100% cpu 9.072 total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment