blob: 4c6d5ddf245768343323739eb26274a31d840aba [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>
Anders Lewisa7b0f882017-07-24 20:01:13 -070023#include "util.h"
Christopher Ferrisdf4942c2015-02-17 19:58:53 -080024
Elliott Hughes281e06b2016-02-17 10:23:52 -080025static void BM_time_clock_gettime(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070026 timespec t;
Elliott Hughes281e06b2016-02-17 10:23:52 -080027 while (state.KeepRunning()) {
Elliott Hughes7634db52014-06-09 18:35:21 -070028 clock_gettime(CLOCK_MONOTONIC, &t);
29 }
Elliott Hughes7634db52014-06-09 18:35:21 -070030}
Anders Lewisa7b0f882017-07-24 20:01:13 -070031BIONIC_BENCHMARK(BM_time_clock_gettime);
Elliott Hughes625993d2014-07-15 16:53:13 -070032
Elliott Hughes281e06b2016-02-17 10:23:52 -080033static void BM_time_clock_gettime_syscall(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070034 timespec t;
Elliott Hughes281e06b2016-02-17 10:23:52 -080035 while (state.KeepRunning()) {
Elliott Hughes625993d2014-07-15 16:53:13 -070036 syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t);
37 }
Elliott Hughes625993d2014-07-15 16:53:13 -070038}
Anders Lewisa7b0f882017-07-24 20:01:13 -070039BIONIC_BENCHMARK(BM_time_clock_gettime_syscall);
Elliott Hughes625993d2014-07-15 16:53:13 -070040
Elliott Hughes281e06b2016-02-17 10:23:52 -080041static void BM_time_gettimeofday(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070042 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -080043 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -080044 gettimeofday(&tv, nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -070045 }
Elliott Hughes625993d2014-07-15 16:53:13 -070046}
Anders Lewisa7b0f882017-07-24 20:01:13 -070047BIONIC_BENCHMARK(BM_time_gettimeofday);
Elliott Hughes625993d2014-07-15 16:53:13 -070048
Elliott Hughes281e06b2016-02-17 10:23:52 -080049void BM_time_gettimeofday_syscall(benchmark::State& state) {
Elliott Hughes625993d2014-07-15 16:53:13 -070050 timeval tv;
Elliott Hughes281e06b2016-02-17 10:23:52 -080051 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -080052 syscall(__NR_gettimeofday, &tv, nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -070053 }
Elliott Hughes625993d2014-07-15 16:53:13 -070054}
Anders Lewisa7b0f882017-07-24 20:01:13 -070055BIONIC_BENCHMARK(BM_time_gettimeofday_syscall);
Elliott Hughes625993d2014-07-15 16:53:13 -070056
Elliott Hughes281e06b2016-02-17 10:23:52 -080057void BM_time_time(benchmark::State& state) {
58 while (state.KeepRunning()) {
Elliott Hughesea877162017-01-11 14:34:16 -080059 time(nullptr);
Elliott Hughes625993d2014-07-15 16:53:13 -070060 }
Elliott Hughes625993d2014-07-15 16:53:13 -070061}
Anders Lewisa7b0f882017-07-24 20:01:13 -070062BIONIC_BENCHMARK(BM_time_time);
Elliott Hughesea877162017-01-11 14:34:16 -080063
64void BM_time_localtime(benchmark::State& state) {
65 time_t t = time(nullptr);
66 while (state.KeepRunning()) {
67 localtime(&t);
68 }
69}
Anders Lewisa7b0f882017-07-24 20:01:13 -070070BIONIC_BENCHMARK(BM_time_localtime);
Elliott Hughesea877162017-01-11 14:34:16 -080071
72void BM_time_localtime_r(benchmark::State& state) {
73 time_t t = time(nullptr);
74 while (state.KeepRunning()) {
75 struct tm tm;
76 localtime_r(&t, &tm);
77 }
78}
Anders Lewisa7b0f882017-07-24 20:01:13 -070079BIONIC_BENCHMARK(BM_time_localtime_r);