time_benchmark.cpp revision 3002131da33401cf1b45abbdbec58b7c751fc43a
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
19#include <sys/syscall.h>
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.
25extern "C" void localtime_tz(const time_t* const timep, struct tm* tmp, const char* tz);
26
27static 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}
38BENCHMARK(BM_time_localtime_tz);
39
40#endif
41
42static void BM_time_clock_gettime(int iters) {
43  StartBenchmarkTiming();
44
45  timespec t;
46  for (int i = 0; i < iters; ++i) {
47    clock_gettime(CLOCK_MONOTONIC, &t);
48  }
49
50  StopBenchmarkTiming();
51}
52BENCHMARK(BM_time_clock_gettime);
53
54static 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}
64BENCHMARK(BM_time_clock_gettime_syscall);
65
66static 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}
76BENCHMARK(BM_time_gettimeofday);
77
78static 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}
88BENCHMARK(BM_time_gettimeofday_syscall);
89
90static 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}
99BENCHMARK(BM_time_time);
100