-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimeofday.cpp
50 lines (40 loc) · 1.26 KB
/
timeofday.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <benchmark/benchmark.h>
#include <chrono>
#include <time.h>
#include "cpu_utils.hpp"
namespace bm = benchmark;
static void test_c_gettimeofday(bm::State& state)
{
set_cpu_affinity(2);
for (auto _ : state)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
time_t seconds = ts.tv_sec % 86400;
auto hour = seconds / 3600;
auto minute = (seconds % 3600) / 60;
auto second = seconds % 60;
auto us = ts.tv_nsec / 1000;
// Prevent compiler optimizations
benchmark::DoNotOptimize(hour);
benchmark::DoNotOptimize(minute);
benchmark::DoNotOptimize(second);
benchmark::DoNotOptimize(us);
}
}
static void test_cpp_system_clock(bm::State& state)
{
set_cpu_affinity(2);
for (auto _ : state)
{
auto now = std::chrono::system_clock::now();
auto now_t = std::chrono::system_clock::to_time_t(now);
auto us = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) %
std::chrono::seconds(1);
// Prevent compiler optimizations
benchmark::DoNotOptimize(now_t);
benchmark::DoNotOptimize(us);
}
}
BENCHMARK(test_c_gettimeofday);
BENCHMARK(test_cpp_system_clock);