imate
C++/CUDA Reference
Loading...
Searching...
No Matches
Timer Class Reference

Records elasped wall time between two events. More...

#include <timer.h>

Public Member Functions

 Timer ()
 constructor for Timer
 
 ~Timer ()
 Destructor for Timer.
 
void start ()
 Starts the timer.
 
void stop ()
 Stops the timer.
 
double elapsed () const
 Returns the elapsed time in seconds.
 

Static Protected Member Functions

static double get_wall_time ()
 Returns the wall time since the epoch.
 

Protected Attributes

double start_time
 
double stop_time
 

Detailed Description

Records elasped wall time between two events.

The measured time is the wall time, not the process time of the CPU.

Example:

Timer timer;
timer.start();

// Some CPU threads here.
// ...

// Note, this Time measures wall time, so the sleep() time
// counts toward the measured time.
sleep(1);

timer.stop();
double elapsed_time = timer.elapsed();
Note
The start and stop time variables inside this class must be declared as double, and not float to have enough precision for the subtraction of stop minus start time. However, the elapsed time outside of this class can be declared as float.
See also
CudaTimer

Definition at line 49 of file timer.h.

Constructor & Destructor Documentation

◆ Timer()

Timer::Timer ( )

constructor for Timer

Definition at line 40 of file timer.cpp.

40 :
41 start_time(0),
42 stop_time(0)
43{
44}
double start_time
Definition timer.h:64
double stop_time
Definition timer.h:65

◆ ~Timer()

Timer::~Timer ( )

Destructor for Timer.

Definition at line 54 of file timer.cpp.

55{
56}

Member Function Documentation

◆ elapsed()

double Timer::elapsed ( ) const

Returns the elapsed time in seconds.

Definition at line 92 of file timer.cpp.

93{
94 return this->stop_time - this->start_time;
95}

References start_time, and stop_time.

Referenced by cTraceEstimator< DataType >::c_trace_estimator().

Here is the caller graph for this function:

◆ get_wall_time()

double Timer::get_wall_time ( )
staticprotected

Returns the wall time since the epoch.

Definition at line 105 of file timer.cpp.

106{
107 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && \
108 !defined(__CYGWIN__)
109
110 LARGE_INTEGER time, freq;
111 if (!QueryPerformanceFrequency(&freq))
112 {
113 std::runtime_error("Cannot obtain system's time frequency.");
114 return NAN;
115 }
116
117 if (!QueryPerformanceCounter(&time))
118 {
119 std::runtime_error("Cannot obtain elapsed time.");
120 return NAN;
121 }
122
123 return static_cast<double>(time.QuadPart) / \
124 static_cast<double>(freq.QuadPart);
125
126 #elif defined(__unix__) || defined(__unix) || \
127 (defined(__APPLE__) && defined(__MACH__))
128
129 struct timeval time;
130 if (gettimeofday(&time, NULL))
131 {
132 std::runtime_error("Cannot obtain elapsed time.");
133 return NAN;
134 }
135
136 return static_cast<double>(time.tv_sec) + \
137 static_cast<double>(time.tv_usec) * 1e-6;
138
139 #else
140 #error "Unknown compiler"
141 #endif
142}

Referenced by start(), and stop().

Here is the caller graph for this function:

◆ start()

void Timer::start ( )

Starts the timer.

Definition at line 66 of file timer.cpp.

67{
69}
static double get_wall_time()
Returns the wall time since the epoch.
Definition timer.cpp:105

References get_wall_time(), and start_time.

Referenced by cTraceEstimator< DataType >::c_trace_estimator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop()

void Timer::stop ( )

Stops the timer.

Definition at line 79 of file timer.cpp.

80{
82}

References get_wall_time(), and stop_time.

Referenced by cTraceEstimator< DataType >::c_trace_estimator().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ start_time

double Timer::start_time
protected

Definition at line 64 of file timer.h.

Referenced by elapsed(), and start().

◆ stop_time

double Timer::stop_time
protected

Definition at line 65 of file timer.h.

Referenced by elapsed(), and stop().


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