int max = 100000; int flag = 0; Stopwatch sw = new Stopwatch(); sw.Reset(); sw.Start(); // 經過一次裝箱 for (int i = 0; i < max; i++) { string str = " i = " +i; } sw.Stop(); Console.WriteLine("one boxing time:" + sw.Elapsed.TotalMilliseconds.ToString());
sw.Reset(); sw.Start(); // 不裝箱 for (int i = 0; i < max; i++) { string str = " i = " + i.ToString(); } sw.Stop(); Console.WriteLine("no boxing time:" + sw.Elapsed.TotalMilliseconds.ToString());
DateTime now = DateTime.Now;
while (!((DateTime.Now.Second - 5) == now.Second)) { string str = " i = " + flag; flag++; } Console.WriteLine("one boxing 5 second run :" + flag.ToString() + " times");
flag =0; now = DateTime.Now; while (!((DateTime.Now.Second - 5) == now.Second)) { string str = " i = " + flag.ToString(); flag++; } Console.WriteLine("no boxing 5 second run :" + flag.ToString() + " times");
輸出結果
1 2 3 4
one boxing time:12.413 no boxing time:9.1419 one boxing 5 second run :26794548 times no boxing 5 second run :32986447 times