Pol  Revision:4b29d2b
timer.cpp
Go to the documentation of this file.
1 #include "timer.h"
2 
3 #include "logfacility.h"
4 
5 namespace Pol
6 {
7 namespace Tools
8 {
9 void DebugT::print( const std::string& name, long long time )
10 {
11  INFO_PRINT << "\n----------------------------------\n"
12  << name << ": " << time << " ms\n"
13  << "----------------------------------\n";
14 }
15 void SilentT::print( const std::string&, long long ) {}
16 
17 template <class printer>
18 Timer<printer>::Timer( std::string name ) : _name( std::move( name ) )
19 {
20  start();
21 }
22 template <class printer>
24 {
25  start();
26 }
27 template <class printer>
29 {
30  if ( _start == _end )
31  stop();
32  printer::print( _name, ellapsed() );
33 }
34 template <class printer>
36 {
37  _start = _end = Clock::now();
38 }
39 template <class printer>
41 {
42  _end = Clock::now();
43 }
44 
45 template <class printer>
46 long long Timer<printer>::ellapsed() const
47 {
48  Clock::time_point _now = Clock::now();
49  if ( _start != _end ) // already stopped
50  _now = _end;
51  return std::chrono::duration_cast<ms>( _now - _start ).count();
52 }
53 template <class printer>
55 {
56  return ellapsed() * static_cast<double>( ms::period::num ) / ms::period::den;
57 }
58 template <class printer>
60 {
62 }
63 
64 
66 
68 {
69  Clock::time_point _now = Clock::now();
70  return std::chrono::duration_cast<time_mu>( _now - _start );
71 }
72 
73 // forward declarce both versions
74 template class Timer<DebugT>;
75 template class Timer<SilentT>;
76 }
77 }
Clock::time_point _end
Definition: timer.h:48
std::chrono::high_resolution_clock Clock
Definition: timer.h:54
void print() const
Definition: timer.cpp:59
STL namespace.
time_mu ellapsed() const
Definition: timer.cpp:67
std::string _name
Definition: timer.h:46
Clock::time_point _start
Definition: timer.h:47
void start()
Definition: timer.cpp:35
double ellapsed_s() const
Definition: timer.cpp:54
std::chrono::milliseconds ms
Definition: timer.h:32
static void print(const std::string &name, long long time)
Definition: timer.cpp:15
Clock::time_point _start
Definition: timer.h:61
long long ellapsed() const
Definition: timer.cpp:46
std::string name
Definition: osmod.cpp:943
static void print(const std::string &name, long long time)
Definition: timer.cpp:9
std::chrono::microseconds time_mu
Definition: timer.h:55
#define INFO_PRINT
Definition: logfacility.h:223
Definition: berror.cpp:12