CORSIKA  @c8_version@
The framework to simulate particle cascades for astroparticle physics
Timer.hpp
1 /*
2  * (c) Copyright 2020 CORSIKA Project, corsika-project@lists.kit.edu
3  *
4  * This software is distributed under the terms of the GNU General Public
5  * Licence version 3 (GPL Version 3). See file LICENSE for a full version of
6  * the license.
7  */
8 
9 #pragma once
10 
11 #include <chrono>
12 #include <utility>
13 
14 #include <type_traits>
15 
16 namespace corsika {
17 
18  template <typename TClock = std::chrono::high_resolution_clock,
19  typename TDuration = std::chrono::microseconds>
20  class Timer {
21  protected:
23  using clock_type = TClock;
24 
26  using duration_type = TDuration;
27 
29  typename clock_type::time_point start_;
30 
33 
34  public:
35  Timer()
36  : timeDiff_(0){};
37 
39  void startTimer() { start_ = clock_type::now(); }
40 
42  void stopTimer() {
43  timeDiff_ = std::chrono::duration_cast<duration_type>(clock_type::now() - start_);
44  }
45 
51  duration_type getTime() const { return timeDiff_; }
52  };
53 
54  std::false_type is_timer_impl(...);
55  template <typename T, typename U>
56  std::true_type is_timer_impl(Timer<T, U> const volatile&);
57 
58  template <typename T>
59  constexpr bool is_timer_v =
60  std::is_same_v<decltype(is_timer_impl(std::declval<T&>())), std::true_type>;
61 
62 } // namespace corsika
TClock clock_type
Default clock used for time measurement.
Definition: Timer.hpp:23
TDuration duration_type
Internal resolution of the time measurement.
Definition: Timer.hpp:26
void startTimer()
Start the timer.
Definition: Timer.hpp:39
duration_type timeDiff_
Measured runtime of the function.
Definition: Timer.hpp:32
`, since they are used everywhere as integral part of the framework.
clock_type::time_point start_
Startpoint of time measurement.
Definition: Timer.hpp:29
void stopTimer()
Stop the timer.
Definition: Timer.hpp:42
duration_type getTime() const
Returns the runtime of the last call to the wrapped function.
Definition: Timer.hpp:51