CppNoddy  0.92
Loading...
Searching...
No Matches
Public Member Functions | List of all members
CppNoddy::Timer Class Reference

A simple CPU-clock-tick timer for timing metods. More...

#include <Timer.h>

Public Member Functions

 Timer ()
 
 Timer (std::string name)
 
double get_wall_time ()
 
void start ()
 Start the timer & reset stored time to zero. More...
 
void stop ()
 Stop the clock & add the current time interval to the previously stored values ready for printing. More...
 
void reset ()
 Pause the clock & add the time interval to the stored cumulative time. More...
 
void print () const
 Write a string to cout stating the time taken. More...
 
int & counter ()
 Increment an internal discrete counter. More...
 
double time_per_count () const
 
double get_time () const
 Return the time. More...
 

Detailed Description

A simple CPU-clock-tick timer for timing metods.

Definition at line 19 of file Timer.h.

Constructor & Destructor Documentation

◆ Timer() [1/2]

CppNoddy::Timer::Timer ( )
inline

Definition at line 23 of file Timer.h.

23 : STOPPED(true), COUNTER(0),
24 T_START(clock()), DELTA_T_STORE(0), m_deltaWall(0.0) {
25 HEADER = "";
26 m_wallStart = get_wall_time();
27 }
double get_wall_time()
Definition: Timer.h:36

References get_wall_time().

◆ Timer() [2/2]

CppNoddy::Timer::Timer ( std::string  name)
inline

Definition at line 29 of file Timer.h.

29 : STOPPED(true), COUNTER(0),
30 T_START(clock()), DELTA_T_STORE(0), m_deltaWall(0.0) {
31 HEADER = name;
32 m_wallStart = get_wall_time();
33 }

References get_wall_time().

Member Function Documentation

◆ counter()

int & CppNoddy::Timer::counter ( )

Increment an internal discrete counter.

Definition at line 44 of file Timer.cpp.

44 {
45 return COUNTER;
46 }

Referenced by main().

◆ get_time()

double CppNoddy::Timer::get_time ( ) const

Return the time.

Returns
The time (in ms).

Definition at line 34 of file Timer.cpp.

34 {
35 if(STOPPED) {
36 // if stopped or paused return stored intervals
37 return 1.e3 * double(DELTA_T_STORE) / CLOCKS_PER_SEC;
38 } else {
39 // running clock
40 return 1.e3 * (double(DELTA_T_STORE) + double(clock()) - double(T_START)) / CLOCKS_PER_SEC;
41 }
42 }

Referenced by main().

◆ get_wall_time()

double CppNoddy::Timer::get_wall_time ( )
inline

Definition at line 36 of file Timer.h.

36 {
37 struct timeval time;
38 if (gettimeofday(&time,NULL)){
39 // Handle error
40 return 0;
41 }
42 return (double)time.tv_sec + (double)time.tv_usec * .000001;
43 }

Referenced by stop(), and Timer().

◆ print()

void CppNoddy::Timer::print ( ) const

Write a string to cout stating the time taken.

Definition at line 59 of file Timer.cpp.

59 {
60 std::cout.precision(4);
61 std::cout << " " << HEADER << "\n";
62 const double elapsed_time_in_ms(1.e3 * double(DELTA_T_STORE) / CLOCKS_PER_SEC);
63 if(elapsed_time_in_ms > 1000) {
64 std::cout << " TOTAL CPU time taken = " << elapsed_time_in_ms / 1000. << " s\n";
65 std::cout << " TOTAL wall time taken = " << m_deltaWall << " s\n";
66 } else {
67 std::cout << " TOTAL CPU time taken = " << elapsed_time_in_ms << " ms\n";
68 }
69 if(COUNTER != 0) {
70 std::cout << " Number of loops during this time = " << COUNTER << "\n";
71 std::cout << " Throughput = " << 1.e3 * COUNTER / elapsed_time_in_ms << " runs/s \n";
72 }
73 }

Referenced by main().

◆ reset()

void CppNoddy::Timer::reset ( )

Pause the clock & add the time interval to the stored cumulative time.

Definition at line 26 of file Timer.cpp.

26 {
27 stop();
28 COUNTER = 0;
29 STOPPED = true;
30 DELTA_T_STORE = 0;
31 m_deltaWall = 0.0;
32 }
void stop()
Stop the clock & add the current time interval to the previously stored values ready for printing.
Definition: Timer.cpp:17

References stop().

Referenced by main().

◆ start()

void CppNoddy::Timer::start ( )

Start the timer & reset stored time to zero.

Definition at line 12 of file Timer.cpp.

12 {
13 T_START = clock();
14 STOPPED = false;
15 }

Referenced by main().

◆ stop()

void CppNoddy::Timer::stop ( )

Stop the clock & add the current time interval to the previously stored values ready for printing.

Definition at line 17 of file Timer.cpp.

17 {
18 if(!STOPPED) {
19 // we add current time interval to that stored
20 DELTA_T_STORE += clock() - T_START;
21 STOPPED = true;
22 m_deltaWall = get_wall_time() - m_wallStart;
23 }
24 }

References get_wall_time().

Referenced by main(), and reset().

◆ time_per_count()

double CppNoddy::Timer::time_per_count ( ) const
Returns
The average time taken per increment of the internal discrete counter.

Definition at line 48 of file Timer.cpp.

48 {
49 if(STOPPED) {
50 return 1.e3 * double(DELTA_T_STORE) / CLOCKS_PER_SEC / COUNTER;
51 } else {
52 std::string problem = HEADER;
53 problem += "\n The Timer object can only return time_per_count after being stopped.\n";
54 throw ExceptionRuntime(problem);
55 }
56 return 0; // dummy
57 }

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

© 2012

R.E. Hewitt