-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeasure_elapsed.cpp
63 lines (56 loc) · 1.6 KB
/
measure_elapsed.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
#include <benchmark/benchmark.h>
#include <chrono>
#include <ctime>
#include <time.h>
#ifdef __x86_64__
#include <x86intrin.h>
#endif
// Benchmark for std::chrono
static void BM_Chrono(benchmark::State& state) {
for (auto _ : state) {
auto start = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
benchmark::DoNotOptimize(end - start);
}
}
static void BM_Chrono_steady_clock(benchmark::State& state) {
for (auto _ : state) {
auto start = std::chrono::steady_clock::now();
auto end = std::chrono::steady_clock::now();
benchmark::DoNotOptimize(end - start);
}
}
// Benchmark for clock()
static void BM_Clock(benchmark::State& state) {
for (auto _ : state) {
clock_t start = clock();
clock_t end = clock();
benchmark::DoNotOptimize(end - start);
}
}
// Benchmark for clock_gettime()
static void BM_ClockGettime(benchmark::State& state) {
for (auto _ : state) {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC_RAW, &start);
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
benchmark::DoNotOptimize(end.tv_nsec - start.tv_nsec);
}
}
#ifdef __x86_64__
// Benchmark for rdtsc
static void BM_Rdtsc(benchmark::State& state) {
for (auto _ : state) {
unsigned long long start = __rdtsc();
unsigned long long end = __rdtsc();
benchmark::DoNotOptimize(end - start);
}
}
#endif
BENCHMARK(BM_Chrono);
BENCHMARK(BM_Chrono_steady_clock);
BENCHMARK(BM_Clock);
BENCHMARK(BM_ClockGettime);
#ifdef __x86_64__
BENCHMARK(BM_Rdtsc);
#endif