libpnicore
chrono_timer.hpp
1 //
2 // (c) Copyright 2012 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpniio.
5 //
6 // libpniio is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpniio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpniio. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ============================================================================
20 //
21 // Created on: Oct 24, 2012
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 
26 #include "../types.hpp"
27 
28 #include<chrono>
29 #include<iostream>
30 
31 namespace pni{
32 namespace core{
33 
43  template<typename DTYPE> struct duration_unit_map;
44 
45 #define DURATIONUNITMAP(durationtype)\
46  template<> struct duration_unit_map<durationtype>\
47  {\
48  static const string unit;\
49  }
50 
51 #define DURATIONUNITMAPINIT(durationtype,unit_string)\
52  const string duration_unit_map<durationtype>::unit = string(unit_string)
53 
54 
55 
57  DURATIONUNITMAP(std::chrono::nanoseconds);
58  DURATIONUNITMAP(std::chrono::microseconds);
59  DURATIONUNITMAP(std::chrono::milliseconds);
60  DURATIONUNITMAP(std::chrono::seconds);
61  DURATIONUNITMAP(std::chrono::minutes);
62  DURATIONUNITMAP(std::chrono::hours);
64 
72 
91  template<
95  typename CLKT,
96  typename DTYPE
97  >
99  {
100  private:
101  typename CLKT::time_point _start;
102  typename CLKT::time_point _stop;
103 
104  public:
105  //=====================static members===============================
107  static const string name;
108  //=======================public types===============================
110  chrono_timer(): _start(),_stop() {}
111 
112  //===========================public member functions================
114  void start() { _start = CLKT::now(); }
115 
116  //------------------------------------------------------------------
118  void stop() { _stop = CLKT::now(); }
119 
120  //------------------------------------------------------------------
123  {
124  return float64(std::chrono::duration_cast<DTYPE>(_stop-_start).count());
125  }
126 
127  //------------------------------------------------------------------
129  string unit() const
130  {
132  }
133  };
134 
135  //setup the static name of the timer
136  template<typename CLKT,typename DTYPE>
137  const string chrono_timer<CLKT,DTYPE>::name = string("chrono_timer");
138 
139 //end of namespace
140 }
141 }
string unit() const
get the unit of the timer
Definition: chrono_timer.hpp:129
void start()
start the timer
Definition: chrono_timer.hpp:114
timer class using chrono
Definition: chrono_timer.hpp:98
unit map
Definition: chrono_timer.hpp:43
Definition: add_op.hpp:29
CLKT::time_point _start
start time
Definition: chrono_timer.hpp:101
float64 duration() const
get the duration as double
Definition: chrono_timer.hpp:122
chrono_timer()
default constructor
Definition: chrono_timer.hpp:110
CLKT::time_point _stop
end time
Definition: chrono_timer.hpp:102
double float64
64Bit IEEE floating point type
Definition: types/types.hpp:56
static const string name
name of the timer
Definition: chrono_timer.hpp:107
void stop()
stop the timer
Definition: chrono_timer.hpp:118
std::string string
String type.
Definition: types/types.hpp:69