Stopwatch timer, stopwatch C #

.NET2.0 also provides such a stopwatch: Stopwatch class, it can be more precise time measurement.
Speed Test:
performance and scalability of software is a complex subject. To ensure that applications meet user expectations, we need to consider its performance and scalability in the development cycle. This is essential in the design phase, a poor design will almost certainly lead to a bad user experience. However, there are only good design does not guarantee programs to run efficiently, the final code quality is equally important.
Measure a long time to run routine is fairly simple. If a process takes a few minutes, as long as a watch can record its time. For example, a two-minute time execution process, can save 10% improvement in 12 seconds, it is very easy to determine.
And if you want to measure a very short process, we must consider a better accuracy. For example, there are some small routines that time may be running thousandth of a second, but it will be called a million times, so the cumulative effect is obvious. In previous versions of the .NET framework, we need to use the Windows API function, and in the .NET framework 2.0, Microsoft introduced a Stopwatch (it is our stopwatch) class to simplify the measurement task time.
Stopwatch categories:
use Stopwatch class to measure time very simple. Like real life, stopwatch, objects of this class can also be carried out on the counter to start, stop, return to zero (reset) operation, but it is generally comparable stopwatch more precisely, it is accurate to microseconds (that is, one million tenths of a second).
Sample code:
To demonstrate Stopwatch or to use sections of the code it. The following is a console application, all integers between it accumulates the 1-1000000:


the using the System;

namespace StopWatchClass
{
class Program
{
static void the Main ( String [] args)
{

Long Total = 0 ;

for ( int I = . 1 ; I <= 10000000 ; I ++ )
{
Total
+ = I;
}
}
}
}


Add Object Stopwatch:
Stopwatch class located System.Diagnostics namespace. Here is the code after the addition of target:

the using the System;
the using the System.Diagnostics;

namespace StopWatchClass
{
class Program
{
static void the Main ( String [] args)
{
The Stopwatch Timer
= new new The Stopwatch ();
Long Total = 0 ;

for ( int I = . 1 ; I <= 10000000 ; I ++ )
{
Total
+ = I;
}
}
}
}


Control Object Stopwatch:
Stopwatch provides several methods for controlling the object Stopwatch. Start method to start a timer operation, Stop method to stop timing. At this time, if the second method using the Start, will continue timing, the final result is two timing accumulated timing. To avoid this, a method using an object Reset to zero before the second timing. These three methods do not require parameters. Code:

the using the System;
the using the System.Diagnostics;

namespace StopWatchClass
{
class Program
{
static void the Main ( String [] args)
{
The Stopwatch Timer
= new new The Stopwatch ();
Long Total = 0 ;

timer.start ();
for ( int I = . 1 ; I <= 10000000 ; I ++ )
{
Total
+ = I;
}

Timer.stop ();

}
}
}


reads Stopwatch results:
the next step is to read the results after the timer counting. Stopwatch class provides the following properties:

  • Elapsed: Returns a TimeSpan object representing the measured time interval;
  • ElapsedMilliseconds:返回计时经过的微秒数,精确度稍差,适合于稍长一点的计时;
  • ElapsedTicks: 返回计时经过的计时器刻度(timer tick)数。计时器刻度是Stopwatch对象可能的最小量度单位。计时器刻度时间的长度由特定的计算机和操作系统确定。Stopwatch对象的 Frequency静态字段的值表示一秒所包含的计时器刻度数。注意它与TimeSpan的Ticks属性所用的时间单位的区别。
Should be selected according to a property in which case the timing of the task. In our sample program, the Elapsed property provides accuracy required, use it to pass outputs of several microseconds. This is also the highest accuracy of TimeSpan.
The following is the final program code:


the using the System;
the using the System.Diagnostics;

namespace StopWatchClass
{
class Program
{
static void the Main ( String [] args)
{
The Stopwatch Timer
= new new The Stopwatch ();
Long Total = 0 ;

timer.start ();
for ( int I = . 1 ; I <= 10000000 ; I ++ )
{
Total
= + I;
}

Timer.stop ();

decimal Micro = timer.Elapsed.Ticks / 10m;
Console.WriteLine (
" the Execution Time WAS {0:. Microseconds Fl} " , Micro);
}
}
}


In addition, the properties IsRunning You can view a Stopwatch instance is whether the timing, method using StartNew can start a new timer.

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/12/09/2671027.html

Guess you like

Origin blog.csdn.net/weixin_33939380/article/details/93350815