Inefficiency in math library in golang language, maybe type conversion takes a lot of time?

I wrote an encrypted thing today. I used math.Sqrt to find prime numbers. I found that it took a long time. Regardless of whether the following example algorithm has any problems, I wrote a demo to find prime numbers within 10 000 000. Originally this The demo was written in delphi, and I translated it into other languages, so I tested the efficiency of the following languages:

First translate the following code in Go language:

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
}

The output of the GO language above:

664581

40.5965203s

It took about 40 seconds.

 

Then I used this demo of delphi copied from the Internet to execute it:

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;
 Memo1.Lines.Add(TimeToStr(Now-m))    ;
 Memo1.Lines.Add(IntToStr(sum))

end;

Final execution result:

0:00:12
664581
Delphi only took 12 seconds, I feel strange, the execution efficiency of GO language should be faster than that of delphi?

 

I translated it again in JAVA:

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;
	}

 

Results of the:

664581
16.632

JAVA only took 16 seconds.

I translated it again with python

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))

I found that python has no results for a long time. This is also conceivable. After all, it is a scripting language, not as good as interpreted and compiled languages.

At the same time, it is written in C#, and the approximate code is as follows

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;
        }
    }

Under net3.5, the final execution effect of c#: 

664581
00:00:16.9267274

16 seconds, roughly equivalent to JAVA.

 

Logically speaking, the math.sqrt algorithm in the GO language will not be too different from other languages. Why is the execution efficiency so different? So it can only be guessed that the GO language type conversion may waste a lot of time.


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325448518&siteId=291194637