Yangyang's profileZhang YangyangPhotosBlogListsMore ![]() | Help |
|
February 18 Before_field_init and precise semanticusing System;
using System.Collections.Generic; using System.Text; using System.Diagnostics; namespace Test
{ internal sealed class BeforeFieldInit { public static Int32 s_x = 123; } internal sealed class Precise
{ public static Int32 s_x; static Precise() { s_x = 123; } } class Program { static void Main(string[] args) { const Int32 iterations = 1000 * 1000 * 1000; PerfTest1(iterations); PerfTest2(iterations); } private static void PerfTest1(Int32 iterations) { Stopwatch sw = Stopwatch.StartNew(); for (Int32 x = 0; x < iterations; x++) { BeforeFieldInit.s_x = 1; } Console.WriteLine("PrefTest1:{0} BeforeFieldInit", sw.Elapsed); sw = Stopwatch.StartNew();
for (Int32 x = 0; x < iterations; x++) { Precise.s_x = 1; } Console.WriteLine("PrefTest1:{0} Precise", sw.Elapsed); } private static void PerfTest2(Int32 iterations)
{ Stopwatch sw = Stopwatch.StartNew(); for (Int32 x = 0; x < iterations; x++) { BeforeFieldInit.s_x = 1; } Console.WriteLine("PrefTest2:{0} BeforeFieldInit", sw.Elapsed); sw = Stopwatch.StartNew();
for (Int32 x = 0; x < iterations; x++) { Precise.s_x = 1; } Console.WriteLine("PrefTest2:{0} Precise", sw.Elapsed); } } } 输出结果是:
PrefTest1 :0.9545553 BeforeFieldInit
PrefTest1 :4.5477302 Precise
PrefTest2 :0.9380379 BeforeFieldInit
PrefTest2 :0.9407673 Precise |
|
|