In the. NET environment, the precise measurement of the length of time a piece of code is running is significant in network communications, serial communication, and asynchronous operations. Now make a brief summary. The specific code is as follows:
(1), first
Using System.Diagnostics;
(2), main code
Stopwatch SW = new Stopwatch (); Listening cycle 10,000 times required time//timing start SW. Start (); for (int i = 0; i < 10000;i++) {//To do}//Timing end SW. Stop (); Console.WriteLine ("Loop 10,000 times requires \r\n\r\n" + "\ T cycle:" + SW.) Elapsedticks.tostring () + "\ r \ n" + "\ T Duration:" + SW. Elapsed.tostring () + "\ r \ n" + "\ t number of milliseconds:" + SW. Elapsedmilliseconds.tostring ()); Use the timer again//two methods//(1),//time interval 0//sw. Reset (); Start timing//SW. Start (); (2), SW. Restart (); From the beginning, synthesize the above steps for (int i = 0; i < 1000000;i++) {//To do} Timing over SW. Stop (); Console.WriteLine ("Loop 10,000 times requires \r\n\r\n" + "\ T cycle:" + SW.) Elapsedticks.tostring () + "\ r \ n" + "\ T Duration:" + SW. Elapsed.tostring () + "\ r \ n" + "\ t number of milliseconds:" + SW. Elapsedmilliseconds.tostring ()); Console.read ();
(3), Operation effect:
Monitoring code run-time--stopwatch with legal process