C# Class 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".
显示文件 Open project: qwertie/ecsharp

Public Methods

Method Description
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

Private Methods

Method Description
AutoInit ( ) : void

Method Details

ClearAfter() public method

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
return int

EzStopwatch() public method

public EzStopwatch ( bool start ) : System
start bool
return System

Pause() public method

public Pause ( ) : void
return void

Reset() public method

Resets the timer to 0 and pauses it there.
public Reset ( ) : void
return void

Restart() public method

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
return int

Resume() public method

public Resume ( ) : void
return void