C # Stopwatch

Source: Internet
Author: User

question one: the last few days to write the actual application of parallel computing-the Address book, the use of stopwatch to time, found that the timing is the real timing.

Stopwatch Stopwatch =NewStopwatch ();    TimeSpan TimeSpan; Doubletime1,time2; Stopwatch.    Start (); F1 ();//Take time T1stopwatch.    Stop (); TimeSpan=stopwatch.    Elapsed; Time1= Timespan.totalmilliseconds;//time1=t1stopwatch.    Start (); F2 ();//Take time T2stopwatch.    Stop (); TimeSpan=stopwatch.    Elapsed; Time2= Timespan.totalmilliseconds;//Time2=t1+t2

The time of Time2 is t1+t2, which is different from the tim2=t2 I expected ...

Workaround:

1. Use different timers stopwatch and STOPWATCH2, such as code:

Stopwatch Stopwatch =NewStopwatch (); Stopwatch STOPWATCH2=NewStopwatch ();    TimeSpan TimeSpan; Doubletime1,time2; Stopwatch.    Start (); F1 ();//Take time T1stopwatch.    Stop (); TimeSpan=stopwatch.    Elapsed; Time1= Timespan.totalmilliseconds;//time1=t1STOPWATCH2.    Start (); F2 ();//Take time T2STOPWATCH2.    Stop (); TimeSpan=STOPWATCH2.    Elapsed; Time2= Timespan.totalmilliseconds;//Time2=t2

2. Using the Reset function to empty the timer stopwatch is also easy to understand, code slightly.

question two: See code:

Stopwatch Stopwatch =NewStopwatch (); Stopwatch STOPWATCH2=NewStopwatch ();    TimeSpan TimeSpan; Doubletime1,time2; Stopwatch.    Start (); F1 ();//Take time T1stopwatch.    Stop (); TimeSpan=stopwatch.    Elapsed; Time1= Timespan.totalmilliseconds;//time1=t1STOPWATCH2.    Start (); F2 ();//Take time T2
Stopwatch. Stop ();//The STOPWATCH2 should be here. Stop ();
TimeSpan =STOPWATCH2. Elapsed; Time2= Timespan.totalmilliseconds;//Time2=t2

At the end of the second timer STOPWATCH2, Stopwatch2 knocked into the stopwatch, then the problem came, my program has not been affected ...
The reason is that you can query the run time properties when the stopwatch instance is running or stopped.

That's the sentence, that is to say

Although STOPWATCH2 does not have a stop, the sentence in my code is TimeSpan = stopwatch2. Elapsed; The current recorded time is extracted ...

So the time I recorded was not affected, that is, Time1=t1, Time2=t2.

C # Stopwatch

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.