CPoorMansTimingAndProfiling  1.3.6
Instrument C apps simply to profile selected code paths.
 All Data Structures Functions Typedefs Pages
StopWatch Class Reference

Public Member Functions

void addDelta (double delta)
 
void clear ()
 
long elapsedNanos ()
 
StopWatchenableMinMax ()
 
StopWatchenableStdDev ()
 
double estimateStartStopOverhead ()
 
double getAvg ()
 
double getCoefOfVariation ()
 
int getCount ()
 
double getCumulative ()
 
double getMax ()
 
double getMin ()
 
char * getName ()
 
double getStdDev ()
 
bool isStarted ()
 
void printSummary ()
 
void printSummary (FILE *fout)
 
void start ()
 
void stop ()
 
bool stopIfStarted ()
 
 StopWatch (const char *name, const char *file, int line)
 
 ~StopWatch ()
 

Static Public Member Functions

static void clearAll ()
 
static uint64_t currentTimeNanos ()
 
static long determineNanosecondGranularity ()
 
static void dumpTimings (const char *msg)
 
static void dumpTimings (FILE *file, const char *msg)
 
static double estimateGeneralStartStopOverhead ()
 
static void setDumpNormalizationFactor (double factor)
 
static void setMaxNumThreadsToTrack (int n)
 
static void setNestedTiming (bool enable)
 
static void sleepMillis (unsigned long millis)
 

Constructor & Destructor Documentation

StopWatch::StopWatch ( const char *  name,
const char *  file,
int  line 
)

Create a stop watch. Note that name is copied internally. Don't call this directly, use either the macro CREATE_STOP_WATCH or the macro DECLARE_STOP_WATCH.

Parameters
nameof the stop watch
filename of the source file
lineline number in the source file
StopWatch::~StopWatch ( )

Beware that if you have nested timing enabled then there may be references from the call graph to this stop watch – so it should not be destroyed.

Note that if you plan to use dumpTimings() to print the summary of this stop watch then you cannot destroy the stop watch.

Member Function Documentation

void StopWatch::addDelta ( double  delta)

Useful to accumulate your own stats that are not clock based. The specified time is added and the count is incremented by one.

Parameters
deltatime to add in nanoseconds
void StopWatch::clear ( )

Resets the cumulative, count, avg, etc.

static void StopWatch::clearAll ( )
static

Clears all defined stop watches

static uint64_t StopWatch::currentTimeNanos ( )
static

Return the current time as a 64 bit int in nanoseconds.

static long StopWatch::determineNanosecondGranularity ( )
static

Determines the granularity of your system's nanosecond clock by "spinning" in a loop looking for rollover in the time reported by the time methods.

Returns
the resolution in nanoseconds.
static void StopWatch::dumpTimings ( const char *  msg)
static

Simple way to dump the statistics to the console for all stop watches that have been created.

Parameters
msgis output as informative part of the "dump". It may be "" or NULL.
static void StopWatch::dumpTimings ( FILE *  file,
const char *  msg 
)
static

Simple way to dump the statistics to a file for all stop watches that have been created (and not destroyed).

Parameters
fileto write to. Must already be open for write.
msgis output as informative part of the "dump". It may be "" or NULL.
long StopWatch::elapsedNanos ( )

The number of nanoseconds that have elapsed since the stopWatch was started

StopWatch* StopWatch::enableMinMax ( )

Enable min max tracking for the specified stop watch

StopWatch* StopWatch::enableStdDev ( )

Enable computation of standard deviation on the specified stop watch

static double StopWatch::estimateGeneralStartStopOverhead ( )
static

Determine the start/stop overhead of a "general" stop watch. This is a convenience method to avoid having to instantiate a specific stop watch.

Iteratively (2,000,000 times) invokes SWStart() followed by SWStop() to determine the average overhead for these calls. An internal stop watch with no min-max tracking and no standard deviation computation is used.

If you enable nested timing that should be done before this method is invoked so that the effect of tracking nesting can be taken into account.

Returns
the estimated per-start-stop overhead in nanos.
double StopWatch::estimateStartStopOverhead ( )

Determine the start/stop overhead of this stop watch. Iteratively (2,000,000 times) invokes SWStart() followed by SWStop() to determine the average overhead for these calls. This method uses the stopWatch to accumulate statistics. The statistics are not cleared when done. Thus if standard deviation is enabled the stopwatch can be queried after the fact to estimate measurement jitter as well.

If you enable nested timing that should be done before this method is invoked so that the effect of tracking nesting can be taken into account.

Returns
the estimated per-start-stop overhead in nanos.
double StopWatch::getAvg ( )

Compute the average time in ns.

double StopWatch::getCoefOfVariation ( )

Get the coefficient of variation. See http://en.wikipedia.org/wiki/Coefficient_of_variation

int StopWatch::getCount ( )

Get the total number of times the stopwatch has been started

double StopWatch::getCumulative ( )

Get the total time,in ns., that the stopWatch was active.

double StopWatch::getMax ( )

Get the maximum in ns.

double StopWatch::getMin ( )

Get the minimum in ns.

char* StopWatch::getName ( )

Get the name of the stop watch

double StopWatch::getStdDev ( )

Get the standard deviation for the stop watch in ns.

bool StopWatch::isStarted ( )

Has the stop watch started?

Returns
non-zero if the stopwatch is started
void StopWatch::printSummary ( )

Print a summary of the measured time (and statistics) to the console.

void StopWatch::printSummary ( FILE *  fout)

Print a summary of the measured time (and statistics) to a file. If the file argument is null then this prints to the console.

static void StopWatch::setDumpNormalizationFactor ( double  factor)
static

Enable/disable dump statistic normalization. If factor == 0.0 then disable. Otherwise add an output clause to the summaries printed by SWDumpTimings() that lists the total time normalized by the factor. The default value is 0.0.

Parameters
factor
static void StopWatch::setMaxNumThreadsToTrack ( int  n)
static

Set the max number of threads to track. Default is 32. This method is only supported if library is compiled with pthreads support. If there is no pthreads support this method is a NOP.

Parameters
n
static void StopWatch::setNestedTiming ( bool  enable)
static

Enable/disable nested timing. Nesting is disabled by default.

This is intended to be either enabled or disabled for the entire application session. If needed it should be enabled prior to starting any stop watches. The effect of turning this on and off during parts of a run have not been tested.

Parameters
enableset to 1 to enable or 0 to disable
static void StopWatch::sleepMillis ( unsigned long  millis)
static

Sleep the specified number of milliseconds

void StopWatch::start ( )

Start accumulating time on the specified stop watch

void StopWatch::stop ( )

Stop timing

bool StopWatch::stopIfStarted ( )

Stops the stop watch if it has been started, advances the count by one, and adds to the accumulated time. This must be invoked in the same Thread that did the start().

Returns
true if it was started and was stopped