C # knowledge point-StopWatch-timing,
Directory
- Brief Introduction
- Basic usage
- End
Brief Introduction
StopwatchYou can measure the running time at one interval or the total running time at multiple intervals. Generally usedTime used to measure Code ExecutionOrComputing performance dataTo optimize the code performance, you can use Stopwatch to measure the time.
Basic usage
The System. Diagnostics namespace must be referenced during use. First, call the Start method, then call the Stop method, and finally use the Elapsed attribute or use the ElapsedMilliseconds attribute to get the running time (the difference between the two attributes is that the former gets a TimeSpan instance, in milliseconds ). Use IsRunning to determine whether the current status of Stopwatch is running or stopped. The cumulative running time count starts when Start is called each time. The current interval measurement ends when Stop is called each time, and the cumulative running time value is frozen. You can use the Reset method to clear the accumulated running time of an existing Stopwatch instance.
The following example shows how to use the Stopwatch class to determine the execution time of an application:
Stopwatch stopwatch = new Stopwatch (); // The First Time stopwatch. start (); Console. writeLine ("Stopwatch is running: {0}", stopwatch. isRunning); // determines the current Stopwatch status System. threading. thread. sleep (1000); // time-consuming operation stopwatch. stop (); Console. writeLine ("Using Elapsed output runTime: {0}", stopwatch. elapsed. toString (); // The time difference is used to output the Console. writeLine ("Using ElapsedMilliseconds output runTime: {0}", stopwatch. elapsedMilliseconds); // The Console is output in milliseconds. writeLine ("========================================== ================ "); // The second timer stopwatch. start (); System. threading. thread. sleep (1000); // time-consuming operation stopwatch. stop (); Console. writeLine ("The second RunTime: {0}", stopwatch. elapsedMilliseconds); // The Console is output in milliseconds. writeLine ("========================================== ================ "); // The third timer (Restart is used here) stopwatch. restart (); // use Restart here to start the timer (the previous time will be cleared) System. threading. thread. sleep (1000); // time-consuming operation stopwatch. stop (); Console. writeLine ("Using Restart, so runTime: {0}", stopwatch. elapsedMilliseconds); // The Console is output in milliseconds. readKey ();View Code
Running result:
Note: In the program, I Sleep for 1000 ms, but the input result is 1002 for the first time, 2003 for the second time, and 1000 for the last time, there is a certain error related to the computer performance and the current CPU status (here I guess ).
In actual projects, sometimes we cannot use Console. WriteLine () or MessageBox. Show () to get the running time. In this case, we need to write the time into a text file.
The following example shows how to use the Stopwatch class to determine the execution time of an application and output the time to a text file (StreamWriter is used for simplicity ):
Const string path = @ "D: \ StopwatchDemo.txt"; using (StreamWriter streamWriter = new StreamWriter (path, true, Encoding. UTF8) {streamWriter. writeLine ("Start"); streamWriter. flush (); Stopwatch stopwatch = Stopwatch. startNew (); System. threading. thread. sleep (1000); // time-consuming operation stopwatch. stop (); streamWriter. writeLine ("RunTime: {0}", stopwatch. elapsedMilliseconds); // writes data into the file streamWriter in milliseconds. flush ();}View Code
Running result:
End
In C #, the Operations used for timing are not only Stopwatch, but also Timer and TimeSpan. Here is an example of Stopwatch.