/usr/include/dune/common/timer.hh is in libdune-common-dev 2.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set ts=8 sw=2 et sts=2:
#ifndef DUNE_TIMER_HH
#define DUNE_TIMER_HH
#ifndef TIMER_USE_STD_CLOCK
// headers for getrusage(2)
#include <sys/resource.h>
#endif
#include <ctime>
// headers for stderror(3)
#include <cstring>
// access to errno in C++
#include <cerrno>
#include "exceptions.hh"
namespace Dune {
/** @addtogroup Common
@{
*/
/*! \file
\brief A simple timing class.
*/
/** \brief %Exception thrown by the Timer class */
class TimerError : public SystemError {} ;
/** \brief A simple stop watch
This class reports the elapsed user-time, i.e. time spent computing,
after the last call to Timer::reset(). The results are seconds and
fractional seconds. Note that the resolution of the timing depends
on your OS kernel which should be somewhere in the milisecond range.
The class is basically a wrapper for the libc-function getrusage()
*/
class Timer
{
public:
/** \brief A new timer, create and reset
*
* \param startImmediately If true (default) the timer starts counting immediately
*/
Timer (bool startImmediately=true) throw(TimerError)
{
isRunning_ = startImmediately;
reset();
}
//! Reset timer while keeping the running/stopped state
void reset() throw (TimerError)
{
sumElapsed_ = 0.0;
storedLastElapsed_ = 0.0;
rawReset();
}
//! Start the timer and continue measurement if it is not running. Otherwise do nothing.
void start() throw (TimerError)
{
if (not(isRunning_))
{
rawReset();
isRunning_ = true;
}
}
//! Get elapsed user-time from last reset until now/last stop in seconds.
double elapsed () const throw (TimerError)
{
// if timer is running add the time elapsed since last start to sum
if (isRunning_)
return sumElapsed_ + lastElapsed();
return sumElapsed_;
}
//! Get elapsed user-time from last start until now/last stop in seconds.
double lastElapsed () const throw (TimerError)
{
// if timer is running return the current value
if (isRunning_)
return rawElapsed();
// if timer is not running return stored value from last run
return storedLastElapsed_;
}
//! Stop the timer and return elapsed().
double stop() throw (TimerError)
{
if (isRunning_)
{
// update storedLastElapsed_ and sumElapsed_ and stop timer
storedLastElapsed_ = lastElapsed();
sumElapsed_ += storedLastElapsed_;
isRunning_ = false;
}
return elapsed();
}
private:
bool isRunning_;
double sumElapsed_;
double storedLastElapsed_;
#ifdef TIMER_USE_STD_CLOCK
void rawReset() throw (TimerError)
{
cstart = std::clock();
}
double rawElapsed () const throw (TimerError)
{
return (std::clock()-cstart) / static_cast<double>(CLOCKS_PER_SEC);
}
std::clock_t cstart;
#else
void rawReset() throw (TimerError)
{
rusage ru;
if (getrusage(RUSAGE_SELF, &ru))
DUNE_THROW(TimerError, strerror(errno));
cstart = ru.ru_utime;
}
double rawElapsed () const throw (TimerError)
{
rusage ru;
if (getrusage(RUSAGE_SELF, &ru))
DUNE_THROW(TimerError, strerror(errno));
return 1.0 * (ru.ru_utime.tv_sec - cstart.tv_sec) + (ru.ru_utime.tv_usec - cstart.tv_usec) / (1000.0 * 1000.0);
}
struct timeval cstart;
#endif
}; // end class Timer
/** @} end documentation */
} // end namespace
#endif
|