C# Класс Loyc.EzStopwatch

A wrapper around Stopwatch with a more convenient interface, currently oriented around measuring milliseconds (TODO: increase similarity to Stopwatch)
EzStopwatch is a wrapper around the normal Stopwatch that is less clumsy to use: you can get the elapsed time and restart the timer from zero with a single call to Restart(). The Stopwatch class (prior to .NET 4, anyway) requires you to make three separate method calls to do the same thing: you have to call ElapsedMilliseconds, then Reset(), then Start(). Unlike Loyc.Utilities.SimpleTimer, this class does not start timing when it is created, which allows it to be a struct without a constructor. EzStopwatch behaves differently from Stopwatch when restarting, because I observed a problem using the timer to measure short time intervals. I ran trials of three operations in a loop, and the loop was programmed to run until the total elapsed time for one of the operations exceeded 100 ms. On each iteration I restarted the timer three times because there were three operations to measure, and when I replaced SimpleTimer with EzStopwatch for greater accuracy, the loop ran forever! The problem was that (depending on the benchmark's input parameters) the operation could take less than 1 millisecond to complete, so Millisec always returned zero, and the total never reached 100. To solve this problem, when you "Restart" the timer, it is not completely reset to zero, but rather the current value of Millisec is subtracted from the timer. This leaves a fractional amount of time less than 1 millisecond in the timer, so that if you take two measurements that each take 0.6 milliseconds, Millisec will return 0 the first time and 1 the second time, leaving 0.2 milliseconds on the clock. TODO: change interfaces of SimpleTimer and EzStopwatch to better resemble Stopwatch, even though the behavior of "Pause" and "Resume" is more obvious than "Stop" and "Start".
Показать файл Открыть проект

Открытые методы

Метод Описание
ClearAfter ( int minimumMillisec ) : int

Restarts the timer from zero if the specified number of milliseconds have passed, and returns the former value of Millisec.

If this method resets a paused timer, it remains paused but Millisec is set to zero.

EzStopwatch ( bool start ) : System
Pause ( ) : void
Reset ( ) : void

Resets the timer to 0 and pauses it there.

Restart ( ) : int

Restarts the timer from zero (unpausing it if it is paused), and returns the number of elapsed milliseconds prior to the reset.

Resume ( ) : void

Приватные методы

Метод Описание
AutoInit ( ) : void

Описание методов

ClearAfter() публичный Метод

Restarts the timer from zero if the specified number of milliseconds have passed, and returns the former value of Millisec.
If this method resets a paused timer, it remains paused but Millisec is set to zero.
public ClearAfter ( int minimumMillisec ) : int
minimumMillisec int
Результат int

EzStopwatch() публичный Метод

public EzStopwatch ( bool start ) : System
start bool
Результат System

Pause() публичный Метод

public Pause ( ) : void
Результат void

Reset() публичный Метод

Resets the timer to 0 and pauses it there.
public Reset ( ) : void
Результат void

Restart() публичный Метод

Restarts the timer from zero (unpausing it if it is paused), and returns the number of elapsed milliseconds prior to the reset.
public Restart ( ) : int
Результат int

Resume() публичный Метод

public Resume ( ) : void
Результат void