#include <libastrog_timer.h>
A timer can only time one thing at once. To time multiple things that overlap, multiple timer objects should be used.
The timer is cumulative. That is, when you call stop it adds the amount of time passed since it was started to a total. Subsequent starts and stops add to this total. Calling start when it is already started, or stop when it is already stopped will have no effect.
A grain of a timer is the minimum amount of time that it can measure, which is determined by the underlying system time function.
When a timer is instanciated it will check to see if a previous timer has determined the grain of the system function it is using to get the current time. If it has not, it will determine it and set it so subsequent timers can simply look this value up. This means that the first timer you use in your code will cause a slight delay at construction of up to the grain of the system time function (usually a millisecond).
As well as keeping track of the time, the timer class also keeps track of the accuracy of the time measurement. Every start and stop call introduces an error of up to the grain of the system clock funtion. These are added together, and there are a range of accuracy funtions to display this in a variety of formats.
Definition at line 60 of file libastrog_timer.h.
Public Member Functions | |
Timer () | |
Constructor for the Timer class. | |
~Timer () | |
Deconstructor for the Timer class. | |
void | start () |
Starts the timer. | |
void | stop () |
Stops the timer. | |
void | reset () |
Resets the timer. | |
double | get_time () |
Returns the accumulated time in seconds. | |
double | get_error_absolute () |
Returns the accumulated error in seconds. | |
double | get_error_relative () |
Returns the error divided by the time. | |
double | get_error_percentage () |
Returns the error divided by the time expressed as a percentage. | |
Static Public Member Functions | |
static double | get_grain () |
Returns the grain of the system time call. | |
Static Private Member Functions | |
static void | set_grain () |
Determines the grain of the system time function. | |
Private Attributes | |
int | timer_status |
The timer status, ie whether it is currently running. | |
double | time_elapsed |
Previously accumulated time. | |
int | error_grains |
Number of errors introduced by the start and stop calls. | |
timeb | latest_start |
Time of the last start call. | |
timeb | latest_stop |
Time of the last stop call. | |
Static Private Attributes | |
static double | grain = 0 |
The grain of the system time function. |
|
Constructor for the Timer class.
Definition at line 35 of file timer.cpp. References error_grains, set_grain(), time_elapsed, and timer_status. 00036 { 00037 // get grain if it hasn't been determined already 00038 if (this->get_grain()==0.0) { 00039 this->set_grain(); 00040 } 00041 // timer starts off 00042 this->timer_status = 0; 00043 // accumulated time is zero 00044 this->time_elapsed = 0.0; 00045 // zero start/stops so far 00046 this->error_grains = 0; 00047 00048 00049 00050 }
|
|
Deconstructor for the Timer class.
Definition at line 52 of file timer.cpp.
|
|
Returns the accumulated error in seconds.
Definition at line 89 of file timer.cpp. References error_grains, and grain. 00090 { 00091 return this->error_grains*Timer::grain; 00092 }
|
|
Returns the error divided by the time expressed as a percentage.
Definition at line 99 of file timer.cpp. References get_error_relative(). 00100 { 00101 return 100*(this->get_error_relative()); 00102 }
|
|
Returns the error divided by the time.
Definition at line 94 of file timer.cpp. Referenced by get_error_percentage().
|
|
Returns the grain of the system time call.
Definition at line 104 of file timer.cpp. References grain. 00105 { 00106 return Timer::grain; 00107 }
|
|
Returns the accumulated time in seconds.
Definition at line 83 of file timer.cpp. References time_elapsed. Referenced by astrog::PipelineTestTimer::execute(). 00084 { 00085 if (this->timer_status == 1) {} // TODO Throw error 00086 return this->time_elapsed; 00087 }
|
|
Resets the timer.
Definition at line 74 of file timer.cpp. References start(), stop(), and time_elapsed. 00075 { 00076 if (this->timer_status == 1) { 00077 this->stop(); 00078 this->start(); 00079 } 00080 this->time_elapsed = 0; 00081 }
|
|
Determines the grain of the system time function.
Definition at line 109 of file timer.cpp. References grain. Referenced by Timer(). 00110 { 00111 struct timeb start; 00112 struct timeb stop; 00113 ftime(&start); 00114 ftime(&stop); 00115 double time = 0; 00116 while (time<=0) { 00117 ftime(&stop); 00118 time = ((double)(stop.time-start.time)+(double)(stop.millitm-start.millitm)/1000.0f); 00119 } 00120 Timer::grain = time; 00121 }
|
|
Starts the timer. Calling this again once the timer is already started will have no effect on the timer object, and will cause an error event. Definition at line 56 of file timer.cpp. References error_grains, and timer_status. Referenced by astrog::PipelineTestTimer::execute(), and reset(). 00057 { 00058 if (this->timer_status == 1) {} // TODO throw error 00059 this->timer_status = 1; 00060 this->error_grains += 1; 00061 ftime(&this->latest_start); 00062 }
|
|
Stops the timer. Calling this again once the timer is already stopped will have no effect on the timer object, and will cause an error event. Definition at line 64 of file timer.cpp. References error_grains, latest_start, latest_stop, time_elapsed, and timer_status. Referenced by astrog::PipelineTestTimer::execute(), and reset(). 00065 { 00066 if (this->timer_status == 0) {} // TODO throw error 00067 this->timer_status = 0; 00068 this->error_grains += 1; 00069 ftime(&this->latest_stop); 00070 time_elapsed += ((double)(latest_stop.time-latest_start.time) 00071 +((double)(latest_stop.millitm-latest_start.millitm))/1000.0f); 00072 }
|
|
Number of errors introduced by the start and stop calls. See the class description for some more information. Multiply this value by the grain to get the absolute error of the timer. Should be incremented with every start or stop, and set to zero with reset. Definition at line 128 of file libastrog_timer.h. Referenced by get_error_absolute(), start(), stop(), and Timer(). |
|
The grain of the system time function.
Definition at line 104 of file libastrog_timer.h. Referenced by get_error_absolute(), get_grain(), and set_grain(). |
|
Time of the last start call.
Definition at line 132 of file libastrog_timer.h. Referenced by stop(). |
|
Time of the last stop call.
Definition at line 136 of file libastrog_timer.h. Referenced by stop(). |
|
Previously accumulated time. This is the time that previous consecutive start-stop calls have accumulated. If the timer is currently active, the time it has been active must be added to this variable to get the actual amount. Definition at line 119 of file libastrog_timer.h. Referenced by get_time(), reset(), stop(), and Timer(). |
|
The timer status, ie whether it is currently running. 0 : not timing 1 : timing Definition at line 111 of file libastrog_timer.h. |