TBTK
Util.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_UTIL
24 #define COM_DAFER45_TBTK_UTIL
25 
26 #include "Streams.h"
27 
28 #include <vector>
29 #include <chrono>
30 
31 namespace TBTK{
32 namespace Util{
33 
39 class Timer{
40 public:
44  static void tick(std::string tag = "");
45 
48  static void tock();
49 private:
51  static std::vector<std::chrono::time_point<std::chrono::high_resolution_clock>> timestamps;
52 
54  static std::vector<std::string> tags;
55 };
56 
57 inline void Timer::tick(std::string tag){
58  timestamps.push_back(std::chrono::high_resolution_clock::now());
59  tags.push_back(tag);
60 }
61 
62 inline void Timer::tock(){
63  std::chrono::time_point<std::chrono::high_resolution_clock> stop = std::chrono::high_resolution_clock::now();
64  if(timestamps.size() > 0){
65  std::chrono::time_point<std::chrono::high_resolution_clock> start = timestamps.back();
66  timestamps.pop_back();
67  std::string tag = tags.back();
68  tags.pop_back();
69 
70  int hours = (std::chrono::duration_cast<std::chrono::hours>(stop - start).count());
71  int minutes = (std::chrono::duration_cast<std::chrono::minutes>(stop - start).count())%60;
72  int seconds = (std::chrono::duration_cast<std::chrono::seconds>(stop - start).count())%60;
73  int milliseconds = (std::chrono::duration_cast<std::chrono::milliseconds>(stop - start).count())%1000;
74  int microseconds = (std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count())%1000;
75  int nanoseconds = (std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start).count())%1000;
76 
77  Streams::out << "(" << timestamps.size() << ") ";
78  if(hours > 0)
79  Streams::out << hours << "h ";
80  if(hours > 0 || minutes > 0)
81  Streams::out << minutes << "m ";
82  if(hours > 0 || minutes > 0 || seconds > 0)
83  Streams::out << seconds << "s ";
84  if(hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0)
85  Streams::out << milliseconds << "ms ";
86  if(hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0 || microseconds > 0)
87  Streams::out << microseconds << "us ";
88  if(hours > 0 || minutes > 0 || seconds > 0 || milliseconds > 0 || microseconds > 0 || nanoseconds > 0)
89  Streams::out << nanoseconds << "ns ";
90  Streams::out << "\t" << tag << "\n";
91  }
92  else{
93  Streams::out << "Error in Time::tock(): No corresponding tick call made.\n";
94  }
95 }
96 
97 }; //End of namespace Util
98 }; //End of namespace TBTK
99 
100 #endif
Definition: Util.h:39
static void tick(std::string tag="")
Definition: Util.h:57
static void tock()
Definition: Util.h:62
static std::ostream out
Definition: Streams.h:37
Definition: AbstractOperator.h:26
Streams for TBTK output.