Grid 0.7.0
Timer.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/Timer.h
6
7 Copyright (C) 2015
8
9Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
10Author: Peter Boyle <paboyle@ph.ed.ac.uk>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27*************************************************************************************/
28/* END LEGAL */
29#ifndef GRID_TIME_H
30#define GRID_TIME_H
31
32#include <sys/time.h>
33#include <ctime>
34#include <chrono>
35
37
38//typedef std::chrono::system_clock GridClock;
39typedef std::chrono::high_resolution_clock GridClock;
40typedef std::chrono::time_point<GridClock> GridTimePoint;
41
42typedef std::chrono::seconds GridSecs;
43typedef std::chrono::milliseconds GridMillisecs;
44typedef std::chrono::microseconds GridUsecs;
45typedef std::chrono::microseconds GridTime;
46
48// Dress the output; use std::chrono
49// C++11 time facilities better?
50inline double usecond(void) {
51 auto usecs = std::chrono::duration_cast<GridUsecs>(GridClock::now()-theProgramStart);
52 return 1.0*usecs.count();
53}
54
55
56inline std::ostream& operator<< (std::ostream & stream, const GridSecs & time)
57{
58 stream << time.count()<<" s";
59 return stream;
60}
61inline std::ostream& operator<< (std::ostream & stream, const GridMillisecs & now)
62{
63 GridSecs second(1);
64 auto secs = now/second ;
65 auto subseconds = now%second ;
66 auto fill = stream.fill();
67 stream << secs<<"."<<std::setw(3)<<std::setfill('0')<<subseconds.count()<<" s";
68 stream.fill(fill);
69 return stream;
70}
71inline std::ostream& operator<< (std::ostream & stream, const GridUsecs & now)
72{
73 GridSecs second(1);
74 auto seconds = now/second ;
75 auto subseconds = now%second ;
76 auto fill = stream.fill();
77 stream << seconds<<"."<<std::setw(6)<<std::setfill('0')<<subseconds.count()<<" s";
78 stream.fill(fill);
79 return stream;
80}
81
82
84private:
85 bool running;
88public:
90 Reset();
91 }
92 void Start(void) {
93 assert(running == false);
94#ifdef TIMERS_ON
95 start = GridClock::now();
96#endif
97 running = true;
98 }
99 void Stop(void) {
100 assert(running == true);
101#ifdef TIMERS_ON
102 accumulator+= std::chrono::duration_cast<GridUsecs>(GridClock::now()-start);
103#endif
104 running = false;
105 };
106 void Reset(void){
107 running = false;
108#ifdef TIMERS_ON
109 start = GridClock::now();
110#endif
111 accumulator = std::chrono::duration_cast<GridUsecs>(start-start);
112 }
113 GridTime Elapsed(void) const {
114 assert(running == false);
115 return std::chrono::duration_cast<GridTime>( accumulator );
116 }
117 uint64_t useconds(void) const {
118 assert(running == false);
119 return (uint64_t) accumulator.count();
120 }
121 bool isRunning(void) const {
122 return running;
123 }
124};
125
127
128#endif
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
GridTimePoint theProgramStart
Definition PerfCount.cc:35
std::chrono::high_resolution_clock GridClock
Definition Timer.h:39
std::ostream & operator<<(std::ostream &stream, const GridSecs &time)
Definition Timer.h:56
std::chrono::microseconds GridUsecs
Definition Timer.h:44
std::chrono::microseconds GridTime
Definition Timer.h:45
std::chrono::time_point< GridClock > GridTimePoint
Definition Timer.h:40
std::chrono::milliseconds GridMillisecs
Definition Timer.h:43
std::chrono::seconds GridSecs
Definition Timer.h:42
double usecond(void)
Definition Timer.h:50
void Start(void)
Definition Timer.h:92
GridTimePoint start
Definition Timer.h:86
GridTime Elapsed(void) const
Definition Timer.h:113
GridUsecs accumulator
Definition Timer.h:87
uint64_t useconds(void) const
Definition Timer.h:117
bool running
Definition Timer.h:85
GridStopWatch()
Definition Timer.h:89
bool isRunning(void) const
Definition Timer.h:121
void Stop(void)
Definition Timer.h:99
void Reset(void)
Definition Timer.h:106