-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
leap_benchmark.cpp
83 lines (70 loc) · 2.19 KB
/
leap_benchmark.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
#include <benchmark/benchmark.h>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <limits>
#include <random>
#include <vector>
#include "boost/date_time/gregorian/gregorian_types.hpp"
static std::vector<int> generate_data(size_t size)
{
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<> distribution(1400, 9999);
std::vector<int> data(size);
std::generate(data.begin(), data.end(), [&distribution, &generator]() { return distribution(generator); });
return data;
}
const int limit = 1000;
const std::vector<int> data{generate_data(limit)};
bool result = true;
static void BM_Leap_boolean_chain(benchmark::State& state) {
for(auto _ : state) {
for (size_t i = 0; i < limit; ++i) {
result = (data.at(i) % 4 == 0) && (data.at(i) % 100 != 0 || data.at(i) % 400 == 0);
}
}
}
BENCHMARK(BM_Leap_boolean_chain);
static void BM_Leap_boolean_chain_inverse(benchmark::State& state) {
for(auto _ : state) {
for (size_t i = 0; i < limit; ++i) {
result = (data.at(i) % 400 == 0) || (data.at(i) % 100 != 0 && data.at(i) % 4 == 0);
}
}
}
BENCHMARK(BM_Leap_boolean_chain_inverse);
static void BM_Leap_ternary(benchmark::State& state) {
for(auto _ : state) {
for (size_t i = 0; i < limit; ++i) {
result = data.at(i) % 100 == 0 ? data.at(i) % 400 == 0 : data.at(i) % 4 == 0;
}
}
}
BENCHMARK(BM_Leap_ternary);
static void BM_Leap_chrono(benchmark::State& state) {
for(auto _ : state) {
for (size_t i = 0; i < limit; ++i) {
result = std::chrono::year{data.at(i)}.is_leap();
}
}
}
BENCHMARK(BM_Leap_chrono);
static void BM_Leap_boost(benchmark::State& state) {
for(auto _ : state) {
for (size_t i = 0; i < limit; ++i) {
result = boost::gregorian::gregorian_calendar::is_leap_year(data.at(i));
}
}
}
BENCHMARK(BM_Leap_boost);
// How much does the iteration of the array cost?
static void BM_empty_read(benchmark::State& state) {
for(auto _ : state) {
for (size_t i = 0; i < limit; ++i) {
result = true;
}
}
}
BENCHMARK(BM_empty_read);
BENCHMARK_MAIN();