Date: Mon, 31 May 2021 13:17:25 +0000
Hi!
There should be a function template 'measure' in the standard library which measures the execution time of another function.
'measure' takes the function to execute and the arguments to apply the function to. It returns the execution time and the result of the function call as a struct.
Since functions returning void can't be passed to 'measure' (because they have no result) there should also be a function 'measure_ignore_return' and an extra specialisation of 'measure' for void.
Possible implementation:
template <typename Duration, typename Result>
struct measurement_result {
Duration duration;
Result result;
};
template <
typename Clock = std::chrono::high_resolution_clock,
typename F, typename... Args>
auto measure_ignore_result(F f, Args&&... args)
{
auto const start = Clock::now();
f(std::forward<Args>(args)...);
auto const stop = Clock::now();
auto const duration = stop - start;
return measurement_result<decltype(duration), std::monostate>{duration};
}
template <
typename Clock = std::chrono::high_resolution_clock,
typename F, typename... Args,
std::enable_if_t<std::is_void_v<std::result_of_t<F(Args...)>>, bool> = true>
auto measure(F f, Args&&... args)
{
return measure_ignore_result(
std::forward<F>(f), std::forward<Args>(args)...);
}
template <
typename Clock = std::chrono::high_resolution_clock,
typename F, typename... Args,
std::enable_if_t<!std::is_void_v<std::result_of_t<F(Args...)>>, bool> = true>
auto measure(F f, Args&&... args)
{
auto const start = Clock::now();
auto const result = f(std::forward<Args>(args)...);
auto const stop = Clock::now();
auto const duration = stop - start;
return measurement_result{duration, result};
}
Usage:
auto fib(int i) -> int
{
return i <= 1 ? i : fib(i-1) + fib(i-2);
}
auto main() -> int
{
std::cout << std::chrono::duration<double>(measure(fib, 42).duration).count();
}
Should I write a proposal for that, or is there something like that already in boost? I didn't find it anywhere.
Paul
There should be a function template 'measure' in the standard library which measures the execution time of another function.
'measure' takes the function to execute and the arguments to apply the function to. It returns the execution time and the result of the function call as a struct.
Since functions returning void can't be passed to 'measure' (because they have no result) there should also be a function 'measure_ignore_return' and an extra specialisation of 'measure' for void.
Possible implementation:
template <typename Duration, typename Result>
struct measurement_result {
Duration duration;
Result result;
};
template <
typename Clock = std::chrono::high_resolution_clock,
typename F, typename... Args>
auto measure_ignore_result(F f, Args&&... args)
{
auto const start = Clock::now();
f(std::forward<Args>(args)...);
auto const stop = Clock::now();
auto const duration = stop - start;
return measurement_result<decltype(duration), std::monostate>{duration};
}
template <
typename Clock = std::chrono::high_resolution_clock,
typename F, typename... Args,
std::enable_if_t<std::is_void_v<std::result_of_t<F(Args...)>>, bool> = true>
auto measure(F f, Args&&... args)
{
return measure_ignore_result(
std::forward<F>(f), std::forward<Args>(args)...);
}
template <
typename Clock = std::chrono::high_resolution_clock,
typename F, typename... Args,
std::enable_if_t<!std::is_void_v<std::result_of_t<F(Args...)>>, bool> = true>
auto measure(F f, Args&&... args)
{
auto const start = Clock::now();
auto const result = f(std::forward<Args>(args)...);
auto const stop = Clock::now();
auto const duration = stop - start;
return measurement_result{duration, result};
}
Usage:
auto fib(int i) -> int
{
return i <= 1 ? i : fib(i-1) + fib(i-2);
}
auto main() -> int
{
std::cout << std::chrono::duration<double>(measure(fib, 42).duration).count();
}
Should I write a proposal for that, or is there something like that already in boost? I didn't find it anywhere.
Paul
Received on 2021-05-31 08:17:39