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