NumCpp  2.10.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
timeit.hpp
Go to the documentation of this file.
1 #pragma once
29 
30 #include <chrono>
31 #include <iostream>
32 #include <type_traits>
33 
34 #include "NumCpp/Core/Timer.hpp"
35 #include "NumCpp/Core/Types.hpp"
36 
37 namespace nc::utils
38 {
39  namespace timeit_detail
40  {
42  template<typename TimeUnit>
43  struct Result
44  {
45  TimeUnit min{};
46  TimeUnit max{};
47  TimeUnit mean{};
48  };
49 
50  template<typename TimeUnit>
51  std::ostream& operator<<(std::ostream& os, const Result<TimeUnit> result)
52  {
53  std::string unit{};
54  if constexpr (std::is_same<TimeUnit, std::chrono::hours>::value)
55  {
56  unit = " hours";
57  }
58  else if constexpr (std::is_same<TimeUnit, std::chrono::minutes>::value)
59  {
60  unit = " minutes";
61  }
62  else if constexpr (std::is_same<TimeUnit, std::chrono::seconds>::value)
63  {
64  unit = " seconds";
65  }
66  else if constexpr (std::is_same<TimeUnit, std::chrono::milliseconds>::value)
67  {
68  unit = " milliseconds";
69  }
70  else if constexpr (std::is_same<TimeUnit, std::chrono::microseconds>::value)
71  {
72  unit = " microseconds";
73  }
74  else if constexpr (std::is_same<TimeUnit, std::chrono::nanoseconds>::value)
75  {
76  unit = " nanoseconds";
77  }
78  else
79  {
80  unit = " time units of some sort";
81  }
82 
83  os << "Timeit results:\n";
84  os << "\tmin: " << result.min.count() << unit << "\n";
85  os << "\tmax: " << result.max.count() << unit << "\n";
86  os << "\tmean: " << result.mean.count() << unit << "\n";
87 
88  return os;
89  }
90  } // namespace timeit_detail
91 
92  //============================================================================
102  template<typename TimeUnit, typename Function, typename... Args>
103  timeit_detail::Result<TimeUnit>
104  timeit(uint32 numIterations, bool printResults, Function function, Args&&... args) noexcept
105  {
106  auto result = timeit_detail::Result<TimeUnit>{};
107  auto timer = Timer<TimeUnit>{};
108 
109  for (uint32 i = 0; i < numIterations; ++i)
110  {
111  if (i == 0)
112  {
113  result.min = TimeUnit::max();
114  }
115 
116  timer.tic();
117 
118  using ResultType = std::invoke_result_t<Function, Args...>;
119  if constexpr (std::is_same_v<ResultType, void>)
120  {
121  function(std::forward<Args>(args)...);
122  }
123  else
124  {
125  // cppcheck-suppress redundantAssignment
126  [[maybe_unused]] const ResultType functionResult = function(std::forward<Args&>(args)...);
127  }
128 
129  const auto elapsedTime = timer.toc(false);
130 
131  result.mean = result.mean + elapsedTime;
132  result.min = std::min(result.min, elapsedTime);
133  result.max = std::max(result.max, elapsedTime);
134  }
135 
136  result.mean = result.mean / numIterations;
137 
138  if (printResults)
139  {
140  std::cout << result;
141  }
142 
143  return result;
144  }
145 } // namespace nc::utils
A timer class for timing code execution.
Definition: Timer.hpp:44
std::ostream & operator<<(std::ostream &os, const Result< TimeUnit > result)
Definition: timeit.hpp:51
Definition: Utils/cube.hpp:33
timeit_detail::Result< TimeUnit > timeit(uint32 numIterations, bool printResults, Function function, Args &&... args) noexcept
Definition: timeit.hpp:104
NdArray< dtype > max(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: max.hpp:44
NdArray< dtype > min(const NdArray< dtype > &inArray, Axis inAxis=Axis::NONE)
Definition: min.hpp:44
std::uint32_t uint32
Definition: Types.hpp:40
Result statistics of a timeit run.
Definition: timeit.hpp:44
TimeUnit max
Definition: timeit.hpp:46
TimeUnit mean
Definition: timeit.hpp:47
TimeUnit min
Definition: timeit.hpp:45