CARLsim  3.1.3
CARLsim: a GPU-accelerated SNN simulator
Stopwatch Class Reference

Utility to measure script execution time (wall-clock time) in milliseconds. More...

#include <stopwatch.h>

Public Member Functions

 Stopwatch (bool startTimer=true)
 Utility to measure script execution time (wall-clock time) in milliseconds. More...
 
 ~Stopwatch ()
 Stopwatch destructor. More...
 
uint64_t getLapTime (const std::string &tag) const
 Looks up the execution time of a lap by its tag. More...
 
uint64_t getLapTime (int index) const
 Looks up the execution time of a lap by its vector index. More...
 
uint64_t lap (const std::string &tag="")
 Ends the current lap and starts a new lap. More...
 
void print (FILE *fileStream=NULL) const
 Prints a summary to file or console. More...
 
void reset ()
 Resets the timer. More...
 
void start (const std::string &tag="")
 Starts/restarts the timer. More...
 
uint64_t stop (bool printMessage=true, FILE *printFile=NULL)
 Stops the timer. More...
 

Detailed Description

This class provides a means to measure script execution time (wall-clock time) with millisecond precision.

The stopwatch can start, stop, and lap. It can be used to time different segments of a simulation; for example, to record the time it took to build a network vs. the time it took to run the network. Every segment (or lap) can be annotated with a user-defined string for easier analysis. The method Stopwatch::lap does also return the execution time for the previous lap. Stopwatch::stop also returns the accumulated record time over all laps.

Code example:

int main() {
// create and start timer (controlled by optional flag startTimer=true)
Stopwatch watch;
// ---------------- CONFIG STATE -------------------
CARLsim sim("HelloWorld", CPU_MODE, USER);
// configure network
// ---------------- SETUP STATE -------------------
// build the network
watch.lap("setupNetwork"); // tag this lap with string "setupNetwork"
sim.setupNetwork();
// ---------------- RUN STATE -------------------
watch.lap("runNetwork"); // tag this lap with string "runNetwork"
sim.runNetwork(10,0);
// print stopwatch summary
watch.stop();
}

Stopping the stopwatch will produce nicely formatted console output indicating the starting and stopping time of each lap, as well as the accumulated time.

------------------------------------------------------------------------
|----------------------------------------------------------------------|
| Tag Start Stop Lap Total |
| start 00:00:00.000 00:00:00.004 00:00:00.004 00:00:00.004 |
| setupNetwork 00:00:00.004 00:00:00.064 00:00:00.060 00:00:00.064 |
| runNetwork 00:00:00.064 00:00:06.651 00:00:06.587 00:00:06.651 |
------------------------------------------------------------------------
Since
v3.1

Definition at line 67 of file stopwatch.h.

Constructor & Destructor Documentation

Stopwatch ( bool  startTimer = true)

This class provides a means to measure script execution time (wall-clock time) with millisecond precision.

By default, calling the constructor will also start the timer. Optionally, startTimer can be set to false. In this case, the first call to Stopwatch::start will start the timer.

Parameters
startTimerflag to indicate whether to automatically start the timer (true) or not (false)
~Stopwatch ( )

The destructor deallocates all data structures.

Member Function Documentation

uint64_t getLapTime ( const std::string &  tag) const

This method looks up a certain lap by its specified tag, and if found, returns the lap's recorded execution time. If the tag cannot be found, a warning is printed and value 0 is returned.

Parameters
taguser-defined string name of the lap to look up
Returns
recorded execution time of certain lap (or 0 if tag invalid)
Note
This method can only be called when the timer is off (i.e., after calling Stopwatch::stop).
See also
Stopwatch::getLapTime(int) const
Since
v3.1
uint64_t getLapTime ( int  index) const

This method looks up a certain lap by its position in the list of all recorded laps (0-indexed). For example, the first lap will have index 0, the second will have index 1, and so on. If the index is valid, the method returns the lap's recorded execution time. If the index is invalid, a warning is printed and value 0 is returned.

Parameters
indexindex of the lap in the list of recorded laps
Returns
recorded execution time of certain lap (or 0 if index invalid)
Note
This method can only be called when the timer is off (i.e., after calling Stopwatch::stop).
See also
Stopwatch::getLapTime(const std::string&) const
Since
v3.1
uint64_t lap ( const std::string &  tag = "")

This method ends the current lap and starts a new lap. The new lap can be given a user-defined string for a tag.

This method is equivalent to calling Stopwatch::stop and Stopwatch::start.

The method will also return the execution time of the just-ended lap.

Parameters
taguser-defined string name for the new lap
Returns
execution time of the just-ended lap
Note
This method can only be called when the timer is on (i.e., after calling Stopwatch::start, but before calling Stopwatch::stop).
See also
Stopwatch::start
Stopwatch::stop
Stopwatch::reset
Since
v3.1
void print ( FILE *  fileStream = NULL) const

This method prints a summary to either file or console, depending on optional argument fileStream. By default, fileStream is NULL, making the stream redirect to stdout.

Example output:

------------------------------------------------------------------------
|----------------------------------------------------------------------|
| Tag Start Stop Lap Total |
| start 00:00:00.000 00:00:00.004 00:00:00.004 00:00:00.004 |
| setupNetwork 00:00:00.004 00:00:00.064 00:00:00.060 00:00:00.064 |
| runNetwork 00:00:00.064 00:00:06.651 00:00:06.587 00:00:06.651 |
------------------------------------------------------------------------

Each lap is presented on its own row, annotated by its tag. The start time of the first lap is always 0. All other times are reported relative to that start time. The time format is: number of hours:number of minutes:number of second.number of milliseconds. The column "Total" contains the recorded execution time accumulated over all laps.

Parameters
fileStreamfile stream where to print the summary (default: NULL, which redirects stream to stdout)
Note
This method can only be called when the timer is off (i.e., after calling Stopwatch::stop).
Since
v3.1
void reset ( )

This method resets the timer, effectively deleting all previously recorded data.

Note
This method can only be called when the timer is off (i.e., after calling Stopwatch::stop).
See also
Stopwatch::start
Stopwatch::lap
Stopwatch::stop
Since
v3.1
void start ( const std::string &  tag = "")

This method starts a new lap. The lap can be given a user-defined string for a tag.

A timer can (theoretically) handle an arbitrary number of start/stop cycles.

Parameters
taguser-defined string name for the new lap
See also
Stopwatch::lap
Stopwatch::stop
Stopwatch::reset
Since
v3.1
uint64_t stop ( bool  printMessage = true,
FILE *  printFile = NULL 
)

This method ends a lap. By default, the method will also print a summary to a file (which is stdout by default). Optionally, the print mechanism can be turned off, or the file stream redirected.

A timer can (theoretically) handle an arbitrary number of start/stop cycles.

The method will also return the total accumulated time (ms) over all recorded laps.

Parameters
printMessageflag to indicate whether to print a record summary (true) or not (false)
printFilefile stream where to print the summary (by default: NULL, redirects to stdout)
Returns
total accumulated recorded time (ms)
Note
Opening and closing the filestream must be handled by the user.
See also
Stopwatch::start
Stopwatch::lap
Stopwatch::reset
Since
v3.1

The documentation for this class was generated from the following file: