aboutsummaryrefslogtreecommitdiffstats
path: root/util/datetime/benchmark/gmtime_r/main.cpp
blob: a3356d780c3b36df70446e19ac718ce87a288c35 (plain) (blame)
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
#include <library/cpp/testing/gbenchmark/benchmark.h>

#include <util/datetime/base.h>
#include <util/random/fast.h>

void BM_GmTimeR(benchmark::State& state) {
    time_t now = TInstant::Now().TimeT();
    struct tm buf {};

    for (auto _ : state) {
        Y_DO_NOT_OPTIMIZE_AWAY(GmTimeR(&now, &buf));
    }
}

void BM_gmtime_r(benchmark::State& state) {
    time_t now = TInstant::Now().TimeT();
    struct tm buf {};

    for (auto _ : state) {
        Y_DO_NOT_OPTIMIZE_AWAY(gmtime_r(&now, &buf));
    }
}

void BM_GmTimeRRandom(benchmark::State& state, TDuration window) {
    time_t now = TInstant::Now().TimeT();
    struct tm buf {};

    TFastRng<ui32> rng(2);
    const size_t range = window.Seconds();
    for (auto _ : state) {
        size_t offset = rng.GenRand() % range;
        time_t v = now - offset;
        Y_DO_NOT_OPTIMIZE_AWAY(GmTimeR(&v, &buf));
    }
}

BENCHMARK(BM_GmTimeR);
BENCHMARK(BM_gmtime_r);
BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_hour, TDuration::Hours(1));
BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_day, TDuration::Days(1));
BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_mount, TDuration::Days(31));
BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_year, TDuration::Days(365));
BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_decade, TDuration::Days(3653));
BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_half_centry, TDuration::Days(18262));