CppNoddy  0.92
Loading...
Searching...
No Matches
Timer.cpp
Go to the documentation of this file.
1/// \file Timer.cpp
2/// Implementation of the CppNoddy Timer object.
3
4#include <ctime>
5#include <iostream>
6
7#include <Timer.h>
8#include <Exceptions.h>
9
10namespace CppNoddy {
11
12 void Timer::start() {
13 T_START = clock();
14 STOPPED = false;
15 }
16
17 void Timer::stop() {
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 }
25
26 void Timer::reset() {
27 stop();
28 COUNTER = 0;
29 STOPPED = true;
30 DELTA_T_STORE = 0;
31 m_deltaWall = 0.0;
32 }
33
34 double Timer::get_time() const {
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 }
43
45 return COUNTER;
46 }
47
48 double Timer::time_per_count() const {
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 }
58
59 void Timer::print() const {
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 }
74
75} // end CppNoddy namespace
The collection of CppNoddy exceptions.
A spec for the CppNoddy Timer object.
A generic runtime exception.
Definition: Exceptions.h:158
double get_time() const
Return the time.
Definition: Timer.cpp:34
double get_wall_time()
Definition: Timer.h:36
void start()
Start the timer & reset stored time to zero.
Definition: Timer.cpp:12
double time_per_count() const
Definition: Timer.cpp:48
int & counter()
Increment an internal discrete counter.
Definition: Timer.cpp:44
void print() const
Write a string to cout stating the time taken.
Definition: Timer.cpp:59
void stop()
Stop the clock & add the current time interval to the previously stored values ready for printing.
Definition: Timer.cpp:17
void reset()
Pause the clock & add the time interval to the stored cumulative time.
Definition: Timer.cpp:26
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt