OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
measure.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _MEASURE_H_
18 #define _MEASURE_H_
19 
20 #include <chrono>
21 #include <iomanip>
22 #include <sstream>
23 #include <type_traits>
24 
25 #include "Logger/Logger.h"
26 
27 extern bool g_enable_debug_timer;
28 
29 template <typename TimeT = std::chrono::milliseconds>
30 struct measure {
31  template <typename F, typename... Args>
32  static typename TimeT::rep execution(F func, Args&&... args) {
33  auto start = std::chrono::steady_clock::now();
34  func(std::forward<Args>(args)...);
35  auto duration =
36  std::chrono::duration_cast<TimeT>(std::chrono::steady_clock::now() - start);
37  return duration.count();
38  }
39 };
40 
41 template <typename Type = std::chrono::steady_clock::time_point>
43  return std::chrono::steady_clock::now();
44 }
45 
46 template <typename Type = std::chrono::steady_clock::time_point,
47  typename TypeR = std::chrono::milliseconds>
48 typename TypeR::rep timer_stop(Type clock_begin) {
49  auto duration =
50  std::chrono::duration_cast<TypeR>(std::chrono::steady_clock::now() - clock_begin);
51  return duration.count();
52 }
53 
54 template <typename Type = std::chrono::steady_clock::time_point,
55  typename TypeR = std::chrono::milliseconds>
56 std::string timer_lap(Type clock_begin, Type& clock_last) {
57  auto now = std::chrono::steady_clock::now();
58  auto overall_duration = (now - clock_begin);
59  auto since_last_duration = (now - clock_last);
60  auto overall = std::chrono::duration_cast<TypeR>(overall_duration);
61  auto since_last = std::chrono::duration_cast<TypeR>(since_last_duration);
62  clock_last = now;
63  // std::string ret(overall.count() + " elapsed " + since_last.count());
64  std::ostringstream oss;
65  oss << overall.count() << " - " << since_last.count();
66  return oss.str();
67 }
68 
69 struct InjectTimer {
70  InjectTimer(std::string const& description, int const& lineNum, std::string const& func)
71  : description_(description), lineNum_(lineNum), func_(func) {
73  start_ = timer_start();
74  LOG(INFO) << "Timer start " << std::setfill(' ') << std::setw(35) << description_
75  << " " << std::setw(35) << func_ << ":" << std::setw(5) << lineNum_;
76  }
77  }
78 
81  LOG(INFO) << "Timer end " << std::setfill(' ') << std::setw(35) << description_
82  << " " << std::setw(35) << func_ << ":" << std::setw(5) << lineNum_
83  << " elapsed " << timer_stop(start_) << " ms";
84  }
85  }
86 
87  std::string description_;
88  int lineNum_;
89  std::string func_;
90 
91  std::chrono::steady_clock::time_point start_;
92 };
93 #define INJECT_TIMER(DESC) InjectTimer DESC(#DESC, __LINE__, __FUNCTION__)
94 
95 template <typename Fn, Fn fn, typename... Args>
96 typename std::result_of<Fn(Args...)>::type time_wrap(Args... args) {
97  InjectTimer t("test", 1, "test");
98  return fn(std::forward<Args>(args)...);
99 }
100 #define TIME_WRAP(FUNC) time_wrap<decltype(&FUNC), &FUNC>
101 
102 #endif // _MEASURE_H_
std::chrono::steady_clock::time_point start_
Definition: measure.h:91
static TimeT::rep execution(F func, Args &&...args)
Definition: measure.h:32
#define LOG(tag)
Definition: Logger.h:188
bool g_enable_debug_timer
Definition: Logger.cpp:17
int lineNum_
Definition: measure.h:88
TypeR::rep timer_stop(Type clock_begin)
Definition: measure.h:48
std::string func_
Definition: measure.h:89
std::string timer_lap(Type clock_begin, Type &clock_last)
Definition: measure.h:56
~InjectTimer()
Definition: measure.h:79
std::string description_
Definition: measure.h:87
string description
Definition: setup.py:36
InjectTimer(std::string const &description, int const &lineNum, std::string const &func)
Definition: measure.h:70
std::result_of< Fn(Args...)>::type time_wrap(Args...args)
Definition: measure.h:96
Type timer_start()
Definition: measure.h:42