golang语言中的math库中效率低下,可能是类型转换花费了大量时间?

大洋的顶端 ·
664581 14.581748s go version go1.9 linux/amd64 版本跑的
#2
更多评论
664581 18.837526499s go -1.8.3-64 -linux 版本编译跑的
#1
没发现有这么慢 package main import( "fmt" "time" "math" ) func main() { fmt.Println("total time: ", time.Now().String()) sum := 0 t1 := time.Now() for i :=0; i <= 10000000; i++ { if isPrime(i) == true { sum += 1 } } fmt.Println("total prime numbers: ", sum) fmt.Println("total time: ", time.Now().String()) fmt.Println(time.Now().Sub(t1)) } func isPrime(n int) bool { end := int(math.Sqrt(float64(n))) for i := 2; i <= end; i++ { if n%i == 0 { return false } } return true } 结果: D:\go-project-space>test.exe total time: 2017-10-26 15:34:18.329 +0800 CST m=+0.006000000 total prime numbers: 664581 total time: 2017-10-26 15:34:36.355 +0800 CST m=+18.032000000 18.013s D:\go-project-space>test.exe total time: 2017-10-26 15:39:11.322 +0800 CST m=+0.005000000 total prime numbers: 664581 total time: 2017-10-26 15:39:29.511 +0800 CST m=+18.194000000 18.171s 版本 go version go1.9.1 windows/amd64
#3