This is a creation in Article, where the information may have evolved or changed.
Today I wrote an encrypted thing and used math. sqrt for prime numbers, found that with a long time, regardless of the following example algorithm whether there is a problem, I wrote a demo to find 10 000 000 or less of the quality of the number, originally this demo is Delphi wrote, I translated it into other languages, so I tested the next few language efficiency:
First translated the following code in the Go language:
func main() {t := time.Now()sum := 0for 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 above go language output results:
664581
40.5965203s
It took about 40 seconds.
Then I use the online copy of the Delphi demo executed the following:
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 am surprised that the go language execution efficiency should be faster than Delphi AH?
I have again translated the following 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;}
Execution Result:
664581
16.632
Java only has 16 seconds.
I used Python again to translate the next
def isPrime(n): result = True end = int(math.sqrt(n) + 1) for i in range(2, end): if n % i == 0: result = False return resultsumC = 0t = (time.time())for i in range(10000000): if isPrime(i): sumC = sumC + 1print(sumC)print((time.time() - t))
The discovery of Python for a long time without results, this is also conceivable, after all, is a scripting language, compared to the interpretation of the language and compiled language.
Also written in C #, 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 effect of C # is:
664581
00:00:16.9267274
16 seconds, roughly equivalent to Java.
According to reason, the go language in the MATH.SQRT algorithm does not differ from other languages too much ah, why the implementation efficiency difference is so big? It is therefore only possible to guess that the Go language type conversion wastes a lot of time.