Monitoring code run-time--stopwatch with legal process

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.