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