First, the Stopwatch class under the System.Diagnostics namespace
//Instantiate a timerStopwatch Watch =NewStopwatch ();//Start TimingWatch. Start ();//here is the run code to be evaluated for(inti =1; I <1000000; i++) {}//Execute the task to be timed//End TimingWatch. Stop ();//gets the total elapsed time (in milliseconds) measured by the current instancestringTime =Watch. Elapsedmilliseconds.tostring ();//Description: Stopwatch provides several methods to control the Stopwatch object. The Start method starts a timing operation, and the Stop method stops timing. To avoid this, zero the object with the Reset method before the second timeConsole.WriteLine ("Elapsed: {0}", watch. Elapsed); Console.WriteLine ("In milliseconds: {0}", watch. Elapsedmilliseconds); Console.WriteLine ("In Timer ticks: {0}", watch. Elapsedticks);
Second, the DateTime class
DateTime StartTime =DateTime.Now; Console.WriteLine ("Started: {0}", StartTime); //Execute the task to be timed for(intI=1; I <100000; i++) {} DateTime stoptime=DateTime.Now; Console.WriteLine ("Stopped: {0}", StopTime); TimeSpan ElapsedTime= StopTime-StartTime; Console.WriteLine ("Elapsed: {0}", ElapsedTime); Console.WriteLine ("In hours:"+elapsedtime.totalhours); Console.WriteLine ("In minutes:"+elapsedtime.totalminutes); Console.WriteLine ("In seconds:"+elapsedtime.totalseconds); Console.WriteLine ("In milliseconds:"+ elapsedtime.totalmilliseconds);
Execution time of statistical code for performance analysis