測試代碼:
C# Release 未優化java
class Program { static long counter = 0; static void Main(string[] args) { for (int m = 0; m < 100; m++) { Stopwatch s = new Stopwatch(); s.Start(); for (long i = 0; i < 200000000; i++) { counter = counter * 2 + 3; } s.Stop(); Console.WriteLine(s.Elapsed.TotalMilliseconds + " ms"); } Console.ReadLine(); } }
Core 2.2
測試
Framework 4.6.1
優化
C# Release 優化.net
class Program { static long counter = 0; static void Main(string[] args) { for (int m = 0; m < 100; m++) { Stopwatch s = new Stopwatch(); s.Start(); long temp = counter; for (long i = 0; i < 200000000; i++) { temp = temp * 2 + 3; counter = temp; } s.Stop(); Console.WriteLine(s.Elapsed.TotalMilliseconds + " ms"); } } }
Core 2.2
Framework 4.6.1
pwa
go 未優化(照抄C#)code
var counter int64 = 0 func ExampleLinq_3() { for m := 0; m < 100; m++ { t1 := time.Now() for i := 0; i < 200000000; i++ { counter = counter*2 + 3 } fmt.Println("用時:", time.Now().Sub(t1)) } // Output: }
go 優化(照抄C#)blog
var counter int64 = 0 func ExampleLinq_4() { for m := 0; m < 100; m++ { t1 := time.Now() temp := counter for i := 0; i < 200000000; i++ { temp = temp*2 + 3 counter = temp } fmt.Println("用時:", time.Now().Sub(t1)) } // Output: }
java 8 未優化(照抄C#)
string
java 8 優化(照抄C#)
it
因爲go沒有靜態變量
class
.net core 非靜態
java 8