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

2017/10/26 05:25

``````func main() {
t := time.Now()
sum := 0

for i := 0; i <= 10000000; i++ {
if isPrime(i) == true {
sum = sum + 1

}
}
fmt.Println(sum)
fmt.Println(time.Now().Sub(t))
}
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
}
``````

664581

40.5965203s

``````function isPrime(number: integer): boolean;
var
iHalf,iCount:integer;
begin
result:=true;
iHalf:=Round(SQRT(number));
for iCount:=2 to iHalf do
begin
if (number mod iCount)=0 then
begin
result:=false;
break;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var i ,sum:integer;
var m:TDateTime;
begin
m:=((Now))    ;
sum:=0;
for i:=0  To 10000000 do
begin
if isPrime(i)=True then
begin
sum:=sum+1;
end;
end;

end;``````

0:00:12
664581
delphi 只用了12秒钟，我感到奇怪，GO语言的执行效率应该快于delphi的啊？

``````public class fdfd {
public static void main(String[] args) {
long t = (System.currentTimeMillis());
int sum = 0;

for (int i = 0; i <= 10000000; i++) {
if (isPrime(i)) {
sum = sum + 1;
}

}
System.out.println(sum);
System.out.println((float) (System.currentTimeMillis() - t) / 1000);
}

public static boolean isPrime(int n) {
int end = (int) Math.sqrt(n);
for (int i = 2; i <= end; i++) {
if (n % i == 0)
return false;
}
return true;
}
``````

664581
16.632

JAVA只有了16秒钟。

``````def isPrime(n):
result = True
end = int(math.sqrt(n) + 1)
for i in range(2, end):
if n % i == 0:
result = False

return result

sumC = 0
t = (time.time())
for i in range(10000000):
if isPrime(i):
sumC = sumC + 1

print(sumC)
print((time.time() - t))``````

``````void Button1Click(object sender, EventArgs e)
{     int sum=0;
TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
for (int i=0;i<=10000000;i++){
if (isPrime(i)==true) {
sum=sum+1;
}
}
TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks);
TimeSpan ts3 = ts1.Subtract(ts2).Duration();
textBox1.Text=sum.ToString()+"\r\n";
textBox1.Text=textBox1.Text+ts3.ToString();
}
bool isPrime(int n) {
int end= (int) Math.Sqrt(n);
for (int i=2;i<=end;i++){
if (n % i==0){
return false;

}
}
return true;
}
}``````

664581
00:00:16.9267274

16秒，大致和JAVA相当。

0
1 收藏

### 作者的其它热门文章

i3 g2030 win7 用楼主的代码只用了6秒
2017/10/28 14:25

2017/10/28 14:23

pprofile 分析一下cpu就知道了
2017/10/28 07:27

2017/10/27 20:20

2017/10/27 14:24

package main

import (
"fmt"
)

func generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'.
}
}

func filter(src <-chan int, dst chan<- int, prime int) {
for i := range src { // Loop over values received from 'src'.
if i%prime != 0 {
dst <- i // Send 'i' to channel 'dst'.
}
}
}

func sieve() {
ch := make(chan int) // Create a new channel.
go generate(ch) // Start generate() as a subprocess.
var iCount int32
for {
prime := <-ch
fmt.Print(prime, "\n")
ch1 := make(chan int)
go filter(ch, ch1, prime)
ch = ch1
if iCount > 9999 {
break
}
iCount++
}
}

func main() {
sieve()
}

2017/10/26 17:51

golang 1.7.3 要16s
2017/10/26 17:06

I7 window10 golang 1.9

22秒，使用int32 代替 int 后 9.4秒

2017/10/26 16:49

cpu 和 内存,go的版本和系统的版本也不说,这样不严谨
2017/10/26 16:35

2017/10/26 12:26

14 评论
1 收藏
0