blob: c90dfadce1d1f616f62f3328ad2306cb61d83c47 [file] [log] [blame]
Elliott Hughes4a05bef2013-03-11 17:17:02 -07001/*
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
Elliott Hughes625993d2014-07-15 16:53:13 -070017#include <sys/syscall.h>
Elliott Hughes212e0e32014-12-01 16:43:51 -080018#include <sys/time.h>
Elliott Hughes4a05bef2013-03-11 17:17:02 -070019#include <time.h>
Dan Albert3fe15152015-08-11 16:46:26 -070020#include <unistd.h>
Elliott Hughes4a05bef2013-03-11 17:17:02 -070021
Elliott Hughes281e06b2016-02-17 10:23:52 -080022#include <benchmark/benchmark.h>
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080023
Elliott Hughes281e06b2016-02-17 10:23:52 -080024static void BM_time_clock_gettime(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070025 timespec t;
Elliott Hughes281e06b2016-02-17 10:23:52 -080026 while (state.KeepRunning()) {
Elliott Hughes7634db52014-06-09 18:35:21 -070027 clock_gettime(CLOCK_MONOTONIC, &t);
28 }
Elliott Hughes7634db52014-06-09 18:35:21 -070029}
Elliott Hughes281e06b2016-02-17 10:23:52 -080030BENCHMARK(BM_time_clock_gettime);
Elliott Hughes625993d2014-07-15 16:53:13 -070031
Elliott Hughes281e06b2016-02-17 10:23:52 -080032static void BM_time_clock_gettime_syscall(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070033 timespec t;
Elliott Hughes281e06b2016-02-17 10:23:52 -080034 while (state.KeepRunning()) {
Elliott Hughes625993d2014-07-15 16:53:13 -070035 syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t);
36 }
Elliott Hughes625993d2014-07-15 16:53:13 -070037}
Elliott Hughes281e06b2016-02-17 10:23:52 -080038BENCHMARK(BM_time_clock_gettime_syscall);
Elliott Hughes625993d2014-07-15 16:53:13 -070039
Elliott Hughes281e06b2016-02-17 10:23:52 -080040static void BM_time_gettimeofday(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070041 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -080042 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -080043 gettimeofday(&tv, nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -070044 }
Elliott Hughes625993d2014-07-15 16:53:13 -070045}
Elliott Hughes281e06b2016-02-17 10:23:52 -080046BENCHMARK(BM_time_gettimeofday);
Elliott Hughes625993d2014-07-15 16:53:13 -070047
Elliott Hughes281e06b2016-02-17 10:23:52 -080048void BM_time_gettimeofday_syscall(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070049 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -080050 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -080051 syscall(__NR_gettimeofday, &tv, nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -070052 }
Elliott Hughes625993d2014-07-15 16:53:13 -070053}
Elliott Hughes281e06b2016-02-17 10:23:52 -080054BENCHMARK(BM_time_gettimeofday_syscall);
Elliott Hughes625993d2014-07-15 16:53:13 -070055
Elliott Hughes281e06b2016-02-17 10:23:52 -080056void BM_time_time(benchmark::State& state) {
57 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -080058 time(nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -070059 }
Elliott Hughes625993d2014-07-15 16:53:13 -070060}
Elliott Hughes281e06b2016-02-17 10:23:52 -080061BENCHMARK(BM_time_time);
Elliott Hughesea877162017-01-11 14:34:16 -080062
63void BM_time_localtime(benchmark::State& state) {
64 time_t t = time(nullptr);
65 while (state.KeepRunning()) {
66 localtime(&t);
67 }
68}
69BENCHMARK(BM_time_localtime);
70
71void 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}
78BENCHMARK(BM_time_localtime_r);