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 | |
| 17 | #include "benchmark.h" |
| 18 | |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 19 | #include <sys/syscall.h> |
Elliott Hughes | 4a05bef | 2013-03-11 17:17:02 -0700 | [diff] [blame] | 20 | #include <time.h> |
| 21 | |
| 22 | #if defined(__BIONIC__) |
| 23 | |
| 24 | // Used by the horrible android.text.format.Time class, which is used by Calendar. http://b/8270865. |
| 25 | extern "C" void localtime_tz(const time_t* const timep, struct tm* tmp, const char* tz); |
| 26 | |
| 27 | static void BM_time_localtime_tz(int iters) { |
| 28 | StartBenchmarkTiming(); |
| 29 | |
| 30 | time_t now(time(NULL)); |
| 31 | tm broken_down_time; |
| 32 | for (int i = 0; i < iters; ++i) { |
| 33 | localtime_tz(&now, &broken_down_time, "Europe/Berlin"); |
| 34 | } |
| 35 | |
| 36 | StopBenchmarkTiming(); |
| 37 | } |
| 38 | BENCHMARK(BM_time_localtime_tz); |
Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 39 | |
Elliott Hughes | 4a05bef | 2013-03-11 17:17:02 -0700 | [diff] [blame] | 40 | #endif |
Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 41 | |
| 42 | static void BM_time_clock_gettime(int iters) { |
| 43 | StartBenchmarkTiming(); |
| 44 | |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 45 | timespec t; |
Elliott Hughes | 7634db5 | 2014-06-09 18:35:21 -0700 | [diff] [blame] | 46 | for (int i = 0; i < iters; ++i) { |
| 47 | clock_gettime(CLOCK_MONOTONIC, &t); |
| 48 | } |
| 49 | |
| 50 | StopBenchmarkTiming(); |
| 51 | } |
| 52 | BENCHMARK(BM_time_clock_gettime); |
Elliott Hughes | 625993d | 2014-07-15 16:53:13 -0700 | [diff] [blame] | 53 | |
| 54 | static void BM_time_clock_gettime_syscall(int iters) { |
| 55 | StartBenchmarkTiming(); |
| 56 | |
| 57 | timespec t; |
| 58 | for (int i = 0; i < iters; ++i) { |
| 59 | syscall(__NR_clock_gettime, CLOCK_MONOTONIC, &t); |
| 60 | } |
| 61 | |
| 62 | StopBenchmarkTiming(); |
| 63 | } |
| 64 | BENCHMARK(BM_time_clock_gettime_syscall); |
| 65 | |
| 66 | static void BM_time_gettimeofday(int iters) { |
| 67 | StartBenchmarkTiming(); |
| 68 | |
| 69 | timeval tv; |
| 70 | for (int i = 0; i < iters; ++i) { |
| 71 | gettimeofday(&tv, NULL); |
| 72 | } |
| 73 | |
| 74 | StopBenchmarkTiming(); |
| 75 | } |
| 76 | BENCHMARK(BM_time_gettimeofday); |
| 77 | |
| 78 | static void BM_time_gettimeofday_syscall(int iters) { |
| 79 | StartBenchmarkTiming(); |
| 80 | |
| 81 | timeval tv; |
| 82 | for (int i = 0; i < iters; ++i) { |
| 83 | syscall(__NR_gettimeofday, &tv, NULL); |
| 84 | } |
| 85 | |
| 86 | StopBenchmarkTiming(); |
| 87 | } |
| 88 | BENCHMARK(BM_time_gettimeofday_syscall); |
| 89 | |
| 90 | static void BM_time_time(int iters) { |
| 91 | StartBenchmarkTiming(); |
| 92 | |
| 93 | for (int i = 0; i < iters; ++i) { |
| 94 | time(NULL); |
| 95 | } |
| 96 | |
| 97 | StopBenchmarkTiming(); |
| 98 | } |
| 99 | BENCHMARK(BM_time_time); |