psim  1.0
Generalized 2D phonon transport using a Monte Carlo method
timer.h
Go to the documentation of this file.
1 #ifndef PSIM_TIMER_H
2 #define PSIM_TIMER_H
3 
4 #include <chrono>
5 #include <functional>
6 #include <iostream>
7 #include <string>
8 
9 
10 class Timer {
11 public:
13  : last(std::chrono::steady_clock::now()) {
14  }
15 
16  double get_time_diff() {
17  auto now = std::chrono::steady_clock::now();
18  std::chrono::duration<double> diff = now - last;
19  last = std::chrono::steady_clock::now();
20  return diff.count();
21  }
22 
23  void time(const std::string& msg = "Time Taken: ") {
24  auto now = std::chrono::steady_clock::now();
25  std::chrono::duration<double> diff = now - last;
26  std::cout << msg << diff.count() << "[s]\n";
27  last = std::chrono::steady_clock::now();
28  }
29 
30 private:
31  std::chrono::steady_clock::time_point last;
32 };
33 
34 template<typename Time = std::chrono::microseconds, typename Clock = std::chrono::high_resolution_clock>
35 struct FuncTimer {
36  template<typename F, typename... Args> static Time duration(F&& f, Args... args) {// NOLINT
37  auto start = Clock::now();
38  std::invoke(std::forward<F>(f), std::forward<Args>(args)...);
39  auto end = Clock::now();
40  return std::chrono::duration_cast<Time>(end - start);
41  }
42 };
43 
44 #endif// PSIM_TIMER_H
Definition: timer.h:10
double get_time_diff()
Definition: timer.h:16
void time(const std::string &msg="Time Taken: ")
Definition: timer.h:23
Timer()
Definition: timer.h:12
Definition: timer.h:35
static Time duration(F &&f, Args... args)
Definition: timer.h:36