Botan 3.7.1
Crypto and TLS for C&
time_utils.h
Go to the documentation of this file.
1/**
2* (C) 2024 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_TIME_UTILS_H_
8#define BOTAN_TIME_UTILS_H_
9
10#include <botan/exceptn.h>
11
12#if defined(BOTAN_HAS_OS_UTILS)
13 #include <botan/internal/os_utils.h>
14#endif
15
16#include <chrono>
17
18namespace Botan {
19
20template <typename F>
21uint64_t measure_cost(std::chrono::milliseconds trial_msec, F func) {
22#if defined(BOTAN_HAS_OS_UTILS)
23 const uint64_t trial_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(trial_msec).count();
24
25 uint64_t total_nsec = 0;
26 uint64_t trials = 0;
27
28 auto trial_start = OS::get_system_timestamp_ns();
29
30 for(;;) {
31 const auto start = OS::get_system_timestamp_ns();
32 func();
33 const auto end = OS::get_system_timestamp_ns();
34
35 if(end >= start) {
36 total_nsec += (end - start);
37 trials += 1;
38
39 if((end - trial_start) >= trial_nsec) {
40 return (total_nsec / trials);
41 }
42 }
43 }
44
45#else
46 BOTAN_UNUSED(trial_msec, func);
47 throw Not_Implemented("No system clock available");
48#endif
49}
50
51} // namespace Botan
52
53#endif
#define BOTAN_UNUSED
Definition assert.h:118
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition os_utils.cpp:326
uint64_t measure_cost(std::chrono::milliseconds trial_msec, F func)
Definition time_utils.h:21