7#elif __has_include(<unistd.h>)
13int64_t getProcessCpuTime() {
15 FILETIME creation_time, exit_time, kernel_time, user_time;
16 GetProcessTimes(GetCurrentProcess(), &creation_time, &exit_time,
17 &kernel_time, &user_time);
18 return static_cast<int64_t
>(user_time.dwHighDateTime) << 32 |
19 user_time.dwLowDateTime;
20#elif defined(_POSIX_CPUTIME) && _POSIX_CPUTIME > 0
21 struct timespec cpu_time;
22 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &cpu_time);
23 return static_cast<int64_t
>(cpu_time.tv_sec) * 1'000'000'000 +
26 using clock_duration =
27 std::chrono::duration<std::clock_t, std::ratio<1, CLOCKS_PER_SEC>>;
28 return std::chrono::duration_cast<std::chrono::nanoseconds>(
29 clock_duration{std::clock()})
39 using millis_f64 = std::chrono::duration<double, std::milli>;
40 auto wall_ms = millis_f64(t.
wall_time).count();
41 auto cpu_ms = millis_f64(t.
cpu_time).count();
42 auto cpu_pct = 100 * cpu_ms / wall_ms;
43 auto prec = os.precision(6);
44 os << std::setw(8) << wall_ms <<
" ms (wall) ─ "
45 << std::setw(8) << cpu_ms <<
" ms (CPU) ─ "
46 << std::setprecision(5) << std::setw(7) << cpu_pct <<
"%";
57 auto wall_end_time = clock::now();
58 auto cpu_end_time = getProcessCpuTime();
59 ++
time.num_invocations;
std::chrono::nanoseconds wall_time
std::chrono::nanoseconds cpu_time
std::ostream & operator<<(std::ostream &, TimingsCPU)
Measures the number of invocations of a specific piece of code and its run time.
clock::time_point wall_start_time
CPU and wall-time measurements with RAII helper.