DryChem 1.0.0
A generic, compile-time C++ toolbox with no dependencies for the modern computational chemistry project.
Loading...
Searching...
No Matches
performanceTesting.hpp
Go to the documentation of this file.
1// Copyright (c) 2020-2025 Cody R. Drisko. All rights reserved.
2// Licensed under the MIT License. See the LICENSE file in the project root for more information.
3//
4// Name: performanceTesting.hpp
5// Author: crdrisko
6// Date: 10/20/2020-12:09:44
7// Description: A function that can be used to time other functions
8
9#ifndef DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_UTILITIES_TESTING_PERFORMANCETESTING_HPP
10#define DRYCHEM_COMMON_UTILITIES_INCLUDE_COMMON_UTILS_UTILITIES_TESTING_PERFORMANCETESTING_HPP
11
12#include <chrono>
13#include <functional>
14#include <type_traits>
15#include <utility>
16
18{
19 namespace details
20 {
31 template<typename F, typename... TArgs>
33 {
34 std::invoke_result_t<F, TArgs...> result;
35 std::chrono::microseconds::rep time;
36 };
37 } // namespace details
38
53 template<typename F, typename... TArgs>
54 constexpr decltype(auto) timeAndInvoke(F&& f, TArgs&&... args)
55 {
57 if constexpr (std::is_void_v<std::invoke_result_t<F, TArgs...>>)
58 {
59 auto start = std::chrono::high_resolution_clock::now();
60 std::invoke(std::forward<F>(f), std::forward<TArgs>(args)...);
61 auto end = std::chrono::high_resolution_clock::now();
62
63 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
64
65 return duration;
66 }
67 else
68 {
69 auto start = std::chrono::high_resolution_clock::now();
70 auto result = std::invoke(std::forward<F>(f), std::forward<TArgs>(args)...);
71 auto end = std::chrono::high_resolution_clock::now();
72
73 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
74
75 return details::ReturnType<F, TArgs...> {result, duration};
76 }
77 }
78} // namespace CppUtils::Testing
79
80#endif
Definition performanceTesting.hpp:20
Definition performanceTesting.hpp:18
constexpr decltype(auto) timeAndInvoke(F &&f, TArgs &&... args)
Definition performanceTesting.hpp:54
Definition performanceTesting.hpp:33
std::invoke_result_t< F, TArgs... > result
Definition performanceTesting.hpp:34
std::chrono::microseconds::rep time
Definition performanceTesting.hpp:35