blob: 4a5c2da98eaea2e68990e5048c5b9692ef0b284e [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 Hughes625993d2014-07-15 16:53:13 -070043 gettimeofday(&tv, NULL);
44 }
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 Hughes625993d2014-07-15 16:53:13 -070051 syscall(__NR_gettimeofday, &tv, NULL);
52 }
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 Hughes625993d2014-07-15 16:53:13 -070058 time(NULL);
59 }
Elliott Hughes625993d2014-07-15 16:53:13 -070060}
Elliott Hughes281e06b2016-02-17 10:23:52 -080061BENCHMARK(BM_time_time);