1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-14 01:35:36 +00:00
Grid/lib/perfmon/Timer.h

103 lines
2.8 KiB
C
Raw Normal View History

/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/Timer.h
Copyright (C) 2015
Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory
*************************************************************************************/
/* END LEGAL */
#ifndef GRID_TIME_H
#define GRID_TIME_H
#include <sys/time.h>
#include <ctime>
#include <chrono>
namespace Grid {
// Dress the output; use std::chrono
// C++11 time facilities better?
2016-06-03 11:24:26 +01:00
inline double usecond(void) {
struct timeval tv;
#ifdef TIMERS_ON
gettimeofday(&tv,NULL);
#endif
return 1.0*tv.tv_usec + 1.0e6*tv.tv_sec;
}
typedef std::chrono::system_clock GridClock;
typedef std::chrono::time_point<GridClock> GridTimePoint;
typedef std::chrono::milliseconds GridTime;
2016-03-16 09:28:51 +00:00
typedef std::chrono::microseconds GridUsecs;
inline std::ostream& operator<< (std::ostream & stream, const std::chrono::milliseconds & time)
{
stream << time.count()<<" ms";
return stream;
}
class GridStopWatch {
private:
bool running;
GridTimePoint start;
2016-03-16 09:28:51 +00:00
GridUsecs accumulator;
public:
GridStopWatch () {
Reset();
}
void Start(void) {
assert(running == false);
2016-06-03 11:24:26 +01:00
#ifdef TIMERS_ON
start = GridClock::now();
2016-06-03 11:24:26 +01:00
#endif
running = true;
}
void Stop(void) {
assert(running == true);
2016-06-03 11:24:26 +01:00
#ifdef TIMERS_ON
2016-03-16 09:28:51 +00:00
accumulator+= std::chrono::duration_cast<GridUsecs>(GridClock::now()-start);
2016-06-03 11:24:26 +01:00
#endif
running = false;
};
void Reset(void){
running = false;
2016-06-03 11:24:26 +01:00
#ifdef TIMERS_ON
start = GridClock::now();
2016-06-03 11:24:26 +01:00
#endif
2016-03-16 09:28:51 +00:00
accumulator = std::chrono::duration_cast<GridUsecs>(start-start);
}
GridTime Elapsed(void) {
assert(running == false);
2016-03-16 09:28:51 +00:00
return std::chrono::duration_cast<GridTime>( accumulator );
}
uint64_t useconds(void){
assert(running == false);
return (uint64_t) accumulator.count();
}
};
}
#endif