• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Abseil Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //      https://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 #include "absl/time/clock.h"
15 
16 #if !defined(_WIN32)
17 #include <sys/time.h>
18 #else
19 #include <winsock2.h>
20 #endif  // _WIN32
21 #include <cstdio>
22 
23 #include "absl/base/internal/cycleclock.h"
24 #include "benchmark/benchmark.h"
25 
26 namespace {
27 
BM_Clock_Now_AbslTime(benchmark::State & state)28 void BM_Clock_Now_AbslTime(benchmark::State& state) {
29   while (state.KeepRunning()) {
30     benchmark::DoNotOptimize(absl::Now());
31   }
32 }
33 BENCHMARK(BM_Clock_Now_AbslTime);
34 
BM_Clock_Now_GetCurrentTimeNanos(benchmark::State & state)35 void BM_Clock_Now_GetCurrentTimeNanos(benchmark::State& state) {
36   while (state.KeepRunning()) {
37     benchmark::DoNotOptimize(absl::GetCurrentTimeNanos());
38   }
39 }
40 BENCHMARK(BM_Clock_Now_GetCurrentTimeNanos);
41 
BM_Clock_Now_AbslTime_ToUnixNanos(benchmark::State & state)42 void BM_Clock_Now_AbslTime_ToUnixNanos(benchmark::State& state) {
43   while (state.KeepRunning()) {
44     benchmark::DoNotOptimize(absl::ToUnixNanos(absl::Now()));
45   }
46 }
47 BENCHMARK(BM_Clock_Now_AbslTime_ToUnixNanos);
48 
BM_Clock_Now_CycleClock(benchmark::State & state)49 void BM_Clock_Now_CycleClock(benchmark::State& state) {
50   while (state.KeepRunning()) {
51     benchmark::DoNotOptimize(absl::base_internal::CycleClock::Now());
52   }
53 }
54 BENCHMARK(BM_Clock_Now_CycleClock);
55 
56 #if !defined(_WIN32)
BM_Clock_Now_gettimeofday(benchmark::State & state)57 static void BM_Clock_Now_gettimeofday(benchmark::State& state) {
58   struct timeval tv;
59   while (state.KeepRunning()) {
60     benchmark::DoNotOptimize(gettimeofday(&tv, nullptr));
61   }
62 }
63 BENCHMARK(BM_Clock_Now_gettimeofday);
64 
BM_Clock_Now_clock_gettime(benchmark::State & state)65 static void BM_Clock_Now_clock_gettime(benchmark::State& state) {
66   struct timespec ts;
67   while (state.KeepRunning()) {
68     benchmark::DoNotOptimize(clock_gettime(CLOCK_REALTIME, &ts));
69   }
70 }
71 BENCHMARK(BM_Clock_Now_clock_gettime);
72 #endif  // _WIN32
73 
74 }  // namespace
75