python
#这里python引入了计算加速模块jit
#pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numba
from numba import jit
@jit
def fib(x):
if x<2:
return x
return fib(x-1)+fib(x-2)
if __name__ == '__main__':
import time
begin = time.time()
print(fib(40))
end = time.time()
print(str(end-begin)+"S")
go
package main
import "time"
import "fmt"
func main() {
const n = 40
starttime := time.Now()
fibN := fib(n)
endtime := time.Now()
cost_time := endtime.Sub(starttime)
fmt.Println(cost_time)
fmt.Printf("\rFibonacci(%d) = %d\n", n, fibN)
}
func fib(x int) int {
if x < 2 {
return x
}
return fib(x-1) + fib(x-2)
}
php
<?php
function fib($x){
if($x<2){
return $x;
}
return fib($x-1)+fib($x-2);
}
$begin = mktime(date("Y-m-d H:i:s"));
print_r(fib(40)."\n");
$end = mktime(date("Y-m-d H:i:s"));
echo ($end-$begin)."s";
?>
|