110,535
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
const Int32 i = 2000 * 1000 * 1000;
PerfTest1(i);
PerfTest2(i);
Console.ReadKey();
}
private static void PerfTest1(Int32 i)
{
Stopwatch sw = Stopwatch.StartNew();
for (Int32 x = 0; x < i; x++)
{
BeforeFieldInit.s_x = i;
}
Console.WriteLine("PerfTest1: {0} BeforeFieldInit", sw.Elapsed);
sw = Stopwatch.StartNew();
for (Int32 x = 0; x < i; x++)
{
Precise.s_x = i;
}
Console.WriteLine("PerfTest1: {0} Percise", sw.Elapsed);
}
private static void PerfTest2(Int32 i)
{
Stopwatch sw = Stopwatch.StartNew();
for (Int32 x = 0; x < i; x++)
{
BeforeFieldInit.s_x = i;
}
Console.WriteLine("PerfTest2: {0} BeforeFieldInit", sw.Elapsed);
sw = Stopwatch.StartNew();
for (Int32 x = 0; x < i; x++)
{
Precise.s_x = i;
}
Console.WriteLine("PerfTest2: {0} Percise", sw.Elapsed);
}
}
internal sealed class BeforeFieldInit
{
public static Int32 s_x = 123;
}
internal sealed class Precise
{
public static Int32 s_x;
static Precise()
{
s_x = 123;
}
}