1//===----------------------------------------------------------------------===//
2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3// See https://llvm.org/LICENSE.txt for license information.
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5//
6//===----------------------------------------------------------------------===//
7
8// UNSUPPORTED: c++03, c++11, c++14, c++17
9// UNSUPPORTED: no-filesystem, no-localization, no-tzdb
10
11// XFAIL: libcpp-has-no-experimental-tzdb
12// XFAIL: availability-tzdb-missing
13
14#include <chrono>
15
16#include "benchmark/benchmark.h"
17
18// Benchmarks the performance of the UTC <-> system time conversions. These
19// operations determine the sum of leap second insertions at a specific time.
20
21static void BM_from_sys(benchmark::State& state) {
22 std::chrono::sys_days date{std::chrono::July / 1 / state.range(0)};
23 for (auto _ : state)
24 benchmark::DoNotOptimize(std::chrono::utc_clock::from_sys(date));
25}
26
27BENCHMARK(BM_from_sys)
28 ->Arg(1970) // before the first leap seconds
29 ->Arg(1979) // in the first half of inserted leap seconds
30 ->Arg(1993) // in the second half of inserted leap seconds
31 ->Arg(2100); // after the last leap second
32
33BENCHMARK(BM_from_sys)->Arg(1970)->Arg(1979)->Arg(1993)->Arg(2100)->Threads(4);
34BENCHMARK(BM_from_sys)->Arg(1970)->Arg(1979)->Arg(1993)->Arg(2100)->Threads(16);
35
36static void BM_to_sys(benchmark::State& state) {
37 // 59 sec offset means we pass th UTC offset for the leap second; assuming
38 // there won't be more than 59 leap seconds ever.
39 std::chrono::utc_seconds date{
40 std::chrono::sys_days{std::chrono::July / 1 / state.range(0)}.time_since_epoch() + std::chrono::seconds{59}};
41 for (auto _ : state)
42 benchmark::DoNotOptimize(std::chrono::utc_clock::to_sys(date));
43}
44
45BENCHMARK(BM_to_sys)
46 ->Arg(1970) // before the first leap seconds
47 ->Arg(1979) // in the first half of inserted leap seconds
48 ->Arg(1993) // in the second half of inserted leap seconds
49 ->Arg(2100); // after the last leap second
50
51BENCHMARK(BM_to_sys)->Arg(1970)->Arg(1979)->Arg(1993)->Arg(2100)->Threads(4);
52BENCHMARK(BM_to_sys)->Arg(1970)->Arg(1979)->Arg(1993)->Arg(2100)->Threads(16);
53
54int main(int argc, char** argv) {
55 benchmark::Initialize(&argc, argv);
56 if (benchmark::ReportUnrecognizedArguments(argc, argv))
57 return 1;
58
59 benchmark::RunSpecifiedBenchmarks();
60}
61

source code of libcxx/test/benchmarks/utc_clock.bench.cpp