Heidelberg Educational Numerics Library Version 0.27 (from 15 March 2021)
timer.hh
Go to the documentation of this file.
1#ifndef DUNE_TIMER_HH
2#define DUNE_TIMER_HH
3
4#ifndef TIMER_USE_STD_CLOCK
5// headers for getrusage(2)
6#include <sys/resource.h>
7#endif
8
9#include <ctime>
10
11// headers for stderror(3)
12#include <cstring>
13
14// access to errno in C++
15#include <cerrno>
16
17#include "exceptions.hh"
18
19namespace hdnum {
20
26 class TimerError : public SystemError {} ;
27
28
41class Timer
42{
43public:
46 {
47 reset();
48 }
49
51 void reset()
52 {
53#ifdef TIMER_USE_STD_CLOCK
54 cstart = std::clock();
55#else
56 rusage ru;
59 cstart = ru.ru_utime;
60#endif
61 }
62
64 double elapsed () const
65 {
66#ifdef TIMER_USE_STD_CLOCK
67 return (std::clock()-cstart) / static_cast<double>(CLOCKS_PER_SEC);
68#else
69 rusage ru;
72 return 1.0 * (ru.ru_utime.tv_sec - cstart.tv_sec) + (ru.ru_utime.tv_usec - cstart.tv_usec) / (1000.0 * 1000.0);
73#endif
74 }
75
76private:
77#ifdef TIMER_USE_STD_CLOCK
78 std::clock_t cstart;
79#else
80 struct timeval cstart;
81#endif
82}; // end class Timer
83
84} // end namespace
85
86#endif
Class with mathematical matrix operations.
Definition densematrix.hh:33
Default exception class for OS errors.
Definition exceptions.hh:139
Exception thrown by the Timer class
Definition timer.hh:26
A simple stop watch.
Definition timer.hh:42
double elapsed() const
Get elapsed user-time in seconds.
Definition timer.hh:64
Timer()
A new timer, start immediately.
Definition timer.hh:45
void reset()
Reset timer.
Definition timer.hh:51
A few common exception classes.
#define HDNUM_THROW(E, m)
Definition exceptions.hh:86