php:50长度时请求超时,长度40时用时在一分钟
//实现1000斐波那契数列 $start = microtime(); for($i=0;$i<50;$i++){ echo getfei($i)."<br>"; } function getfei($i){ if ($i<=1){ $fei = 1; }else{ $fei = getfei($i-1)+getfei($i-2); } return $fei; } echo microtime()-$start;
go递归用时:2m23.9160774s
package main import ( "fmt" "time" ) //递归实现斐波那契数列 func main() { t := time.Now() var i int = 0 for i = 0; i < 50; i++ { fe := fei(i) fmt.Printf(" vale is %v\n", fe) } defer func() { cost := time.Since(t) fmt.Println("cost=", cost) }() } func fei(a int) (f int) { if a <= 1 { f = 1 } else { f = fei(a-1) + fei(a-2) } return }
go协程用时:1.9949ms
package main import ( "fmt" "time" ) func feibonacci(ch chan<- int, quit <-chan bool) { x, y := 1, 1 for { select { case ch <- x: x, y = y, x+y case flag := <-quit: fmt.Println("flag =", flag) return } } } func main() { t := time.Now() ch := make(chan int) quit := make(chan bool) go func() { for i := 0; i < 50; i++ { num := <-ch fmt.Println(num) } quit <- true }() feibonacci(ch, quit) defer func() { cost := time.Since(t) fmt.Println("cost=", cost) }() }
综上,php用时最长,go协程用时最短毫秒级的