LOOS 4.1.0
The Lightweight Object Oriented Structural analysis library/toolkit
Loading...
Searching...
No Matches
loos_timer.hpp
1// A simple timer class...
2
3/*
4 This file is part of LOOS.
5
6 LOOS (Lightweight Object-Oriented Structure library)
7 Copyright (c) 2009, Tod D. Romo, Alan Grossfield
8 Department of Biochemistry and Biophysics
9 School of Medicine & Dentistry, University of Rochester
10
11 This package (LOOS) is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation under version 3 of the License.
14
15 This package is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24
25#if !defined(LOOS_TIMER_HPP)
26#define LOOS_TIMER_HPP
27
28#include <iostream>
29#include <string>
30#include <stdexcept>
31
32#include <sys/time.h>
33#include <sys/resource.h>
34#include <boost/format.hpp>
35
36#include <loos_defs.hpp>
37
38
39namespace loos {
40
42 class WallTimer {
43 public:
44 double currentTime(void) const {
45 struct timeval tv;
46
47 int i = gettimeofday(&tv, 0);
48 if (i < 0)
49 throw(std::runtime_error("Error in gettimeofday()"));
50 return(static_cast<double>(tv.tv_sec) + static_cast<double>(tv.tv_usec) * 1e-6);
51 }
52 };
53
55 class UserTimer {
56 public:
57 double currentTime(void) const {
58 struct rusage ru;
59 int i = getrusage(RUSAGE_SELF, &ru);
60 if (i < 0)
61 throw(std::runtime_error("Error in getrusage()"));
62 return(static_cast<double>(ru.ru_utime.tv_sec) + static_cast<double>(ru.ru_utime.tv_usec) * 1e-6);
63 }
64 };
65
66
68
79 template<class TimerType = WallTimer>
80 class Timer : public TimerType {
81 public:
82 Timer() : t0(0), t1(0), avg(0), lapt(0), n(0), running(false) { }
83
85 void start(void) {
86 lapt = t0 = TimerType::currentTime();
87 n=0;
88 avg = 0.0;
89 running = true;
90 }
91
93 // Automatically adds a lap...
94 double stop(void) {
95 t1 = TimerType::currentTime();
96 avg += (t1 - lapt);
97 ++n;
98 running = false;
99 return(t1-t0);
100 }
101
103
109 double elapsed(void) const { return( running ? TimerType::currentTime() - t0 : t1 - t0 ); }
110
112
118 double lap(void) {
119 if (!running) // Don't adjust lap time if the timer ain't running...
120 return(0.0);
121
122 double t = TimerType::currentTime();
123 double lt = t - lapt;
124 lapt = t;
125 avg += lt;
126 ++n;
127 }
128
130 double averageLapTime(void) { return(avg / n); }
131
132 private:
133 double t0, t1, avg, lapt;
134 ulong n;
135 bool running;
136 };
137
138 template<class T>
139 std::ostream& operator<<(std::ostream& os, const Timer<T>& t) {
140 std::string s = timeAsString(t.elapsed());
141 os << "Elapsed time " << s;
142 return(os);
143 }
144}
145
146
147
148
149#endif
Class for tracking time.
Definition loos_timer.hpp:80
void start(void)
Starts the timer.
Definition loos_timer.hpp:85
double stop(void)
Stops the timer.
Definition loos_timer.hpp:94
double elapsed(void) const
Return the elapsed time.
Definition loos_timer.hpp:109
double averageLapTime(void)
Return the current average lap-time...
Definition loos_timer.hpp:130
double lap(void)
Returns the current lap time.
Definition loos_timer.hpp:118
Policy class for tracking only user process time.
Definition loos_timer.hpp:55
Policy class for tracking wall-time.
Definition loos_timer.hpp:42
Namespace for most things not already encapsulated within a class.
Definition version.cpp:3
std::string timeAsString(const double t, const uint precision)
Convert t (seconds) into a string, converting to hours and minutes as necessary.
Definition utils.cpp:203