CppNoddy  0.92
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1/// \file Timer.h
2/// A spec for the CppNoddy Timer object. This is used
3/// to provide course timing of test codes if -DTIME compilation
4/// flag is set.
5
6#ifndef TIMER_H
7#define TIMER_H
8
9#include <ctime>
10#include <string>
11#include <time.h>
12#include <sys/time.h>
13
14namespace CppNoddy {
15
16 /// A simple CPU-clock-tick timer for timing
17 /// metods.
18
19 class Timer {
20
21 public:
22 // CONSTRUCTORS
23 Timer() : 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 }
28
29 Timer(std::string name) : 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 }
34
35 // wall timer
36 double get_wall_time(){
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 }
44
45 // CLASS METHODS
46
47 /// Start the timer & reset stored time to zero.
48 void start();
49
50 /// Stop the clock & add the current time interval to the
51 /// previously stored values ready for printing.
52 void stop();
53
54 /// Pause the clock & add the time interval to the
55 /// stored cumulative time.
56 void reset();
57
58 /// Write a string to cout stating the time taken.
59 void print() const;
60
61 /// Increment an internal discrete counter.
62 int& counter();
63
64 /// \return The average time taken per increment of the
65 /// internal discrete counter.
66 double time_per_count() const;
67
68 /// Return the time.
69 /// \return The time (in ms).
70 double get_time() const;
71
72 private:
73 // ATTRIBUTES
74 bool STOPPED;
75 // a counter - useful for working out time per counter click
76 int COUNTER;
77 // the start time, stop time and time elapsed so far
78 clock_t T_START, DELTA_T_STORE;
79 // wall time
80 double m_wallStart, m_deltaWall;
81 // a string header for the object
82 std::string HEADER;
83 };
84
85}
86
87#endif // TIMER_H
A simple CPU-clock-tick timer for timing metods.
Definition: Timer.h:19
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
Timer(std::string name)
Definition: Timer.h:29
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