| 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 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 22 | #include <benchmark/Benchmark.h> | 
|  | 23 |  | 
|  | 24 | BENCHMARK_NO_ARG(BM_time_clock_gettime); | 
|  | 25 | void BM_time_clock_gettime::Run(int iters) { | 
| Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 26 | StartBenchmarkTiming(); | 
|  | 27 |  | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 28 | timespec t; | 
| Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 29 | for (int i = 0; i < iters; ++i) { | 
|  | 30 | clock_gettime(CLOCK_MONOTONIC, &t); | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 | StopBenchmarkTiming(); | 
|  | 34 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 35 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 36 | BENCHMARK_NO_ARG(BM_time_clock_gettime_syscall); | 
|  | 37 | void BM_time_clock_gettime_syscall::Run(int iters) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 38 | StartBenchmarkTiming(); | 
|  | 39 |  | 
|  | 40 | timespec t; | 
|  | 41 | for (int i = 0; i < iters; ++i) { | 
|  | 42 | syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t); | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | StopBenchmarkTiming(); | 
|  | 46 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 47 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 48 | BENCHMARK_NO_ARG(BM_time_gettimeofday); | 
|  | 49 | void BM_time_gettimeofday::Run(int iters) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 50 | StartBenchmarkTiming(); | 
|  | 51 |  | 
|  | 52 | timeval tv; | 
|  | 53 | for (int i = 0; i < iters; ++i) { | 
|  | 54 | gettimeofday(&tv, NULL); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | StopBenchmarkTiming(); | 
|  | 58 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 59 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 60 | BENCHMARK_NO_ARG(BM_time_gettimeofday_syscall); | 
|  | 61 | void BM_time_gettimeofday_syscall::Run(int iters) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 62 | StartBenchmarkTiming(); | 
|  | 63 |  | 
|  | 64 | timeval tv; | 
|  | 65 | for (int i = 0; i < iters; ++i) { | 
|  | 66 | syscall(__NR_gettimeofday, &tv, NULL); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | StopBenchmarkTiming(); | 
|  | 70 | } | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 71 |  | 
| Christopher Ferris | df4942c | 2015-02-17 19:58:53 -0800 | [diff] [blame] | 72 | BENCHMARK_NO_ARG(BM_time_time); | 
|  | 73 | void BM_time_time::Run(int iters) { | 
| Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 74 | StartBenchmarkTiming(); | 
|  | 75 |  | 
|  | 76 | for (int i = 0; i < iters; ++i) { | 
|  | 77 | time(NULL); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | StopBenchmarkTiming(); | 
|  | 81 | } |