-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.h
More file actions
34 lines (28 loc) · 1015 Bytes
/
timer.h
File metadata and controls
34 lines (28 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
#include <chrono>
#include <concepts>
#include <print>
#include <cstdint>
#include <string_view>
namespace timer {
using clock = std::chrono::high_resolution_clock;
double time(std::invocable auto init, std::invocable auto fn, std::uint32_t repeats) {
auto total = 0.0;
while (repeats--) {
init();
auto start = clock::now();
fn();
auto end = clock::now();
total += std::chrono::duration<double>(end - start).count();
}
return total;
}
void time_print(std::invocable auto init, std::invocable auto fn, std::uint32_t repeats, std::string_view string) {
auto elapsed = time(init, fn, repeats);
std::print("Executing {} {} times took {:.6f} seconds.\n", string, repeats, elapsed);
}
void time_print(std::invocable auto fn, std::uint32_t repeats, std::string_view string) {
auto elapsed = time([](){}, fn, repeats);
std::print("Executing {} {} times took {:.6f} seconds.\n", string, repeats, elapsed);
}
} // namespace timer