1 /*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <sys/syscall.h>
18 #include <sys/time.h>
19 #include <time.h>
20 #include <unistd.h>
21
22 #include <benchmark/benchmark.h>
23
BM_time_clock_gettime(benchmark::State & state)24 static void BM_time_clock_gettime(benchmark::State& state) {
25 timespec t;
26 while (state.KeepRunning()) {
27 clock_gettime(CLOCK_MONOTONIC, &t);
28 }
29 }
30 BENCHMARK(BM_time_clock_gettime);
31
BM_time_clock_gettime_syscall(benchmark::State & state)32 static void BM_time_clock_gettime_syscall(benchmark::State& state) {
33 timespec t;
34 while (state.KeepRunning()) {
35 syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t);
36 }
37 }
38 BENCHMARK(BM_time_clock_gettime_syscall);
39
BM_time_gettimeofday(benchmark::State & state)40 static void BM_time_gettimeofday(benchmark::State& state) {
41 timeval tv;
42 while (state.KeepRunning()) {
43 gettimeofday(&tv, nullptr);
44 }
45 }
46 BENCHMARK(BM_time_gettimeofday);
47
BM_time_gettimeofday_syscall(benchmark::State & state)48 void BM_time_gettimeofday_syscall(benchmark::State& state) {
49 timeval tv;
50 while (state.KeepRunning()) {
51 syscall(__NR_gettimeofday, &tv, nullptr);
52 }
53 }
54 BENCHMARK(BM_time_gettimeofday_syscall);
55
BM_time_time(benchmark::State & state)56 void BM_time_time(benchmark::State& state) {
57 while (state.KeepRunning()) {
58 time(nullptr);
59 }
60 }
61 BENCHMARK(BM_time_time);
62
BM_time_localtime(benchmark::State & state)63 void BM_time_localtime(benchmark::State& state) {
64 time_t t = time(nullptr);
65 while (state.KeepRunning()) {
66 localtime(&t);
67 }
68 }
69 BENCHMARK(BM_time_localtime);
70
BM_time_localtime_r(benchmark::State & state)71 void BM_time_localtime_r(benchmark::State& state) {
72 time_t t = time(nullptr);
73 while (state.KeepRunning()) {
74 struct tm tm;
75 localtime_r(&t, &tm);
76 }
77 }
78 BENCHMARK(BM_time_localtime_r);
79