// fibonacci.c #include <stdio.h> int main() { printf("%d", fibonacci(40)); } int fibonacci(int n) { if (n < 2) { return 1; } return fibonacci(n - 2) + fibonacci(n - 1); }
编译: CFLAGS="-march=corei7-avx -O2 -pipe" && gcc fibonacci.c
运行:time ./a.out
结果:165580141
real 0m0.977s
user 0m0.975s
sys 0m0.002s
// Fibonacci.java public class Fibonacci { public static void main(String[] args) { System.out.println(fibonacci(40)); } public static int fibonacci(int n) { if (n < 2) { return 1; } return fibonacci(n - 2) + fibonacci(n - 1); } }
编译:javac Fibonacci.java
运行:time java Fibonacci
结果:165580141
real 0m0.681s
user 0m0.671s
sys 0m0.016s
// fibonacci.go package main import "fmt" func fibonacci(n int) int { if (n < 2) { return 1 } return fibonacci(n - 2) + fibonacci(n - 1) } func main() { fmt.Println(fibonacci(40)) }
编译:go build fibonacci.go
运行:time ./fibonacci
结果:165580141
real 0m1.175s
user 0m1.173s
sys 0m0.004s
从结果来看:性能 java > C > go 有点怪异哈。
有疑问加站长微信联系(非本文作者)