Botan 3.11.0
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
16namespace Botan {
17
18template <typename F>
19uint64_t measure_cost(uint64_t trial_msec, F func) {
20#if defined(BOTAN_HAS_OS_UTILS)
21 const uint64_t trial_nsec = trial_msec * 1000000;
22
23 uint64_t total_nsec = 0;
24 uint64_t trials = 0;
25
26 auto trial_start = OS::get_system_timestamp_ns();
27
28 for(;;) {
29 const auto start = OS::get_system_timestamp_ns();
30 func();
31 const auto end = OS::get_system_timestamp_ns();
32
33 if(end >= start) {
34 total_nsec += (end - start);
35 trials += 1;
36
37 if((end - trial_start) >= trial_nsec) {
38 return (total_nsec / trials);
39 }
40 }
41 }
42
43#else
44 BOTAN_UNUSED(trial_msec, func);
45 throw Not_Implemented("No system clock available");
46#endif
47}
48
49} // namespace Botan
50
51#endif
#define BOTAN_UNUSED
Definition assert.h:144
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition os_utils.cpp:326
uint64_t measure_cost(uint64_t trial_msec, F func)
Definition time_utils.h:19