Stopwatch class: Http://msdn.microsoft.com/zh-cn/library/system.diagnostics.stopwatch (v=vs.100). aspx
Common Properties and methods:
Start (): Starts or continues to measure the elapsed time of an interval.
Stop (): Stops measuring the elapsed time of an interval.
Elapsedmilliseconds: Gets the total elapsed time (in milliseconds) measured by the current instance.
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Diagnostics;namespaceboxandunbox{classProgram {Static voidMain (string[] args) {Stopwatch Watch=NewStopwatch (); Watch. Start (); Longresult =Sumwithoutbox (); Watch. Stop (); Console.WriteLine ("the Sumwithoutbox () method returns the result of the calculation: {0}, spents {1} milliseconds", result, watch. Elapsedmilliseconds);//gets the total elapsed time (in milliseconds) measured by the current instanceWatch. Restart (); Result=Sumwithbox (); Watch. Stop (); Console.WriteLine ("the Sumwithbox () method returns the result of the calculation: {0}, spents {1} milliseconds", result, watch. Elapsedmilliseconds); Console.readkey (); } Static LongSumwithoutbox () {Longsum =0; for(Longi =0; I <10000000; i++) Sum+=i; returnsum; } Static LongSumwithbox () {Objectsum =0L;//Packing for(Longi =0; I <10000000; i++) Sum= (Long) sum + i;//unpacking, summing, and packing first return(Long) sum;//Unpacking } }}
------------>>>