Enhanced C#
Language of your choice: library documentation
Public fields | Properties | Public Member Functions | List of all members
Loyc.EzStopwatch Struct Reference

A wrapper around Stopwatch with a more convenient interface, currently oriented around measuring milliseconds (TODO: increase similarity to Stopwatch) More...


Source file:

Remarks

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 "<see cref="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".

Public fields

Stopwatch _timer
 
long _offs
 

Properties

int Millisec [get, set]
 Gets or sets the current time on the clock. More...
 
long LongMillisec [get, set]
 
bool Paused [get]
 

Public Member Functions

 EzStopwatch (bool start)
 
void AutoInit ()
 
int Restart ()
 Restarts the timer from zero (unpausing it if it is paused), and returns the number of elapsed milliseconds prior to the reset. More...
 
void Reset ()
 Resets the timer to 0 and pauses it there. More...
 
void Pause ()
 
void Resume ()
 
int ClearAfter (int minimumMillisec)
 Restarts the timer from zero if the specified number of milliseconds have passed, and returns the former value of Millisec. More...
 

Member Function Documentation

int Loyc.EzStopwatch.ClearAfter ( int  minimumMillisec)
inline

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

Returns
If the timer was restarted, this method returns the number of elapsed milliseconds prior to the reset. Returns 0 if the timer was not reset.

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

References Loyc.EzStopwatch.Millisec.

void Loyc.EzStopwatch.Reset ( )
inline

Resets the timer to 0 and pauses it there.

Referenced by Loyc.EzStopwatch.Restart().

int Loyc.EzStopwatch.Restart ( )
inline

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

References Loyc.EzStopwatch.Reset().

Property Documentation

int Loyc.EzStopwatch.Millisec
getset

Gets or sets the current time on the clock.

This property can be used whether the timer is running or not, and it does not affect the value of Paused. It is legal to make the current value negative.

Referenced by Loyc.EzStopwatch.ClearAfter().