| Elliott Hughes | 4a05bef | 2013-03-11 17:17:02 -0700 | [diff] [blame] | 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 |  | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 17 | #include <sys/syscall.h> | 
| Elliott Hughes | 212e0e3 | 2014-12-01 16:43:51 -0800 | [diff] [blame] | 18 | #include <sys/time.h> | 
| Elliott Hughes | 4a05bef | 2013-03-11 17:17:02 -0700 | [diff] [blame] | 19 | #include <time.h> | 
| Dan Albert | 3fe1515 | 2015-08-11 16:46:26 -0700 | [diff] [blame] | 20 | #include <unistd.h> | 
| Elliott Hughes | 4a05bef | 2013-03-11 17:17:02 -0700 | [diff] [blame] | 21 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 22 | #include <benchmark/benchmark.h> | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 23 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 24 | static void BM_time_clock_gettime(benchmark::State& state) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 25 | timespec t; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 26 | while (state.KeepRunning()) { | 
| Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 27 | clock_gettime(CLOCK_MONOTONIC, &t); | 
|  | 28 | } | 
| Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 29 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 30 | BENCHMARK(BM_time_clock_gettime); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 31 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 32 | static void BM_time_clock_gettime_syscall(benchmark::State& state) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 33 | timespec t; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 34 | while (state.KeepRunning()) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 35 | syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t); | 
|  | 36 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 37 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 38 | BENCHMARK(BM_time_clock_gettime_syscall); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 39 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 40 | static void BM_time_gettimeofday(benchmark::State& state) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 41 | timeval tv; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 42 | while (state.KeepRunning()) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 43 | gettimeofday(&tv, NULL); | 
|  | 44 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 45 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 46 | BENCHMARK(BM_time_gettimeofday); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 47 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 48 | void BM_time_gettimeofday_syscall(benchmark::State& state) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 49 | timeval tv; | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 50 | while (state.KeepRunning()) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 51 | syscall(__NR_gettimeofday, &tv, NULL); | 
|  | 52 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 53 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 54 | BENCHMARK(BM_time_gettimeofday_syscall); | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 55 |  | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 56 | void BM_time_time(benchmark::State& state) { | 
|  | 57 | while (state.KeepRunning()) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 58 | time(NULL); | 
|  | 59 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 60 | } | 
| Elliott Hughes | 281e06b | 2016-02-17 10:23:52 -0800 | [diff] [blame] | 61 | BENCHMARK(BM_time_time); |