测试代码:
C# Release 未优化
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 优化
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
go 未优化(照抄C#)
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#)
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#)
java 8 优化(照抄C#)
由于go没有静态变量
.net core 非静态
java 8