-
Notifications
You must be signed in to change notification settings - Fork 1
/
timestamp_bench.cpp
85 lines (72 loc) · 1.8 KB
/
timestamp_bench.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifdef __linux__
#include <sys/time.h>
#endif
#include <ctime>
#include <chrono>
#include <iostream>
#include <fstream>
#include <benchmark/benchmark.h>
namespace {
class timestamp : public benchmark::Fixture {};
BENCHMARK_F(timestamp, rdtsc)(benchmark::State &state) {
uint64_t ts;
uint32_t lo, hi;
for (auto _ : state) {
// RDTSC copies contents of 64-bit TSC into EDX:EAX
asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
ts = static_cast<uint64_t>(hi) << 32 | lo;
}
(void)ts;
}
BENCHMARK_F(timestamp, chrono_system)(benchmark::State &state) {
std::chrono::system_clock::time_point stamp;
for (auto _ : state) {
stamp = std::chrono::system_clock::now();
}
(void)stamp;
}
BENCHMARK_F(timestamp, chrono_steady)(benchmark::State &state) {
std::chrono::steady_clock::time_point stamp;
for (auto _ : state) {
stamp = std::chrono::steady_clock::now();
}
(void)stamp;
}
BENCHMARK_F(timestamp, chrono_high_res)(benchmark::State &state) {
std::chrono::high_resolution_clock::time_point stamp;
for (auto _ : state) {
stamp = std::chrono::high_resolution_clock::now();
}
(void)stamp;
}
BENCHMARK_F(timestamp, clock_get_time_monotonic)(benchmark::State &state) {
timespec tp;
for (auto _ : state) {
clock_gettime(CLOCK_MONOTONIC, &tp);
}
(void)tp;
}
BENCHMARK_F(timestamp, clock_get_time_cpu)(benchmark::State &state) {
timespec tp;
for (auto _ : state) {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
}
(void)tp;
}
BENCHMARK_F(timestamp, clock_realtime)(benchmark::State &state) {
timespec tp;
for (auto _ : state) {
clock_gettime(CLOCK_REALTIME, &tp);
}
(void)tp;
}
#ifdef __linux__
BENCHMARK_F(timestamp, get_time_of_day)(benchmark::State &state) {
timeval tv;
for (auto _ : state) {
gettimeofday(&tv, 0);
}
(void)tv;
}
#endif
} // namespace