time_benchmark.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // Copyright 2018 The Abseil Authors.
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // https://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "absl/time/time.h"
  14. #if !defined(_WIN32)
  15. #include <sys/time.h>
  16. #endif // _WIN32
  17. #include <algorithm>
  18. #include <cmath>
  19. #include <cstddef>
  20. #include <cstring>
  21. #include <ctime>
  22. #include <memory>
  23. #include <string>
  24. #include "absl/time/clock.h"
  25. #include "absl/time/internal/test_util.h"
  26. #include "benchmark/benchmark.h"
  27. namespace {
  28. //
  29. // Addition/Subtraction of a duration
  30. //
  31. void BM_Time_Arithmetic(benchmark::State& state) {
  32. const absl::Duration nano = absl::Nanoseconds(1);
  33. const absl::Duration sec = absl::Seconds(1);
  34. absl::Time t = absl::UnixEpoch();
  35. while (state.KeepRunning()) {
  36. benchmark::DoNotOptimize(t += nano);
  37. benchmark::DoNotOptimize(t -= sec);
  38. }
  39. }
  40. BENCHMARK(BM_Time_Arithmetic);
  41. //
  42. // Time difference
  43. //
  44. void BM_Time_Difference(benchmark::State& state) {
  45. absl::Time start = absl::Now();
  46. absl::Time end = start + absl::Nanoseconds(1);
  47. absl::Duration diff;
  48. while (state.KeepRunning()) {
  49. benchmark::DoNotOptimize(diff += end - start);
  50. }
  51. }
  52. BENCHMARK(BM_Time_Difference);
  53. //
  54. // ToDateTime
  55. //
  56. // In each "ToDateTime" benchmark we switch between two instants
  57. // separated by at least one transition in order to defeat any
  58. // internal caching of previous results (e.g., see local_time_hint_).
  59. //
  60. // The "UTC" variants use UTC instead of the Google/local time zone.
  61. //
  62. void BM_Time_ToDateTime_Absl(benchmark::State& state) {
  63. const absl::TimeZone tz =
  64. absl::time_internal::LoadTimeZone("America/Los_Angeles");
  65. absl::Time t = absl::FromUnixSeconds(1384569027);
  66. absl::Time t2 = absl::FromUnixSeconds(1418962578);
  67. while (state.KeepRunning()) {
  68. std::swap(t, t2);
  69. t += absl::Seconds(1);
  70. benchmark::DoNotOptimize(t.In(tz));
  71. }
  72. }
  73. BENCHMARK(BM_Time_ToDateTime_Absl);
  74. void BM_Time_ToDateTime_Libc(benchmark::State& state) {
  75. // No timezone support, so just use localtime.
  76. time_t t = 1384569027;
  77. time_t t2 = 1418962578;
  78. while (state.KeepRunning()) {
  79. std::swap(t, t2);
  80. t += 1;
  81. struct tm tm;
  82. #if !defined(_WIN32)
  83. benchmark::DoNotOptimize(localtime_r(&t, &tm));
  84. #else // _WIN32
  85. benchmark::DoNotOptimize(localtime_s(&tm, &t));
  86. #endif // _WIN32
  87. }
  88. }
  89. BENCHMARK(BM_Time_ToDateTime_Libc);
  90. void BM_Time_ToDateTimeUTC_Absl(benchmark::State& state) {
  91. const absl::TimeZone tz = absl::UTCTimeZone();
  92. absl::Time t = absl::FromUnixSeconds(1384569027);
  93. while (state.KeepRunning()) {
  94. t += absl::Seconds(1);
  95. benchmark::DoNotOptimize(t.In(tz));
  96. }
  97. }
  98. BENCHMARK(BM_Time_ToDateTimeUTC_Absl);
  99. void BM_Time_ToDateTimeUTC_Libc(benchmark::State& state) {
  100. time_t t = 1384569027;
  101. while (state.KeepRunning()) {
  102. t += 1;
  103. struct tm tm;
  104. #if !defined(_WIN32)
  105. benchmark::DoNotOptimize(gmtime_r(&t, &tm));
  106. #else // _WIN32
  107. benchmark::DoNotOptimize(gmtime_s(&tm, &t));
  108. #endif // _WIN32
  109. }
  110. }
  111. BENCHMARK(BM_Time_ToDateTimeUTC_Libc);
  112. //
  113. // FromUnixMicros
  114. //
  115. void BM_Time_FromUnixMicros(benchmark::State& state) {
  116. int i = 0;
  117. while (state.KeepRunning()) {
  118. benchmark::DoNotOptimize(absl::FromUnixMicros(i));
  119. ++i;
  120. }
  121. }
  122. BENCHMARK(BM_Time_FromUnixMicros);
  123. void BM_Time_ToUnixNanos(benchmark::State& state) {
  124. const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
  125. while (state.KeepRunning()) {
  126. benchmark::DoNotOptimize(ToUnixNanos(t));
  127. }
  128. }
  129. BENCHMARK(BM_Time_ToUnixNanos);
  130. void BM_Time_ToUnixMicros(benchmark::State& state) {
  131. const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
  132. while (state.KeepRunning()) {
  133. benchmark::DoNotOptimize(ToUnixMicros(t));
  134. }
  135. }
  136. BENCHMARK(BM_Time_ToUnixMicros);
  137. void BM_Time_ToUnixMillis(benchmark::State& state) {
  138. const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
  139. while (state.KeepRunning()) {
  140. benchmark::DoNotOptimize(ToUnixMillis(t));
  141. }
  142. }
  143. BENCHMARK(BM_Time_ToUnixMillis);
  144. void BM_Time_ToUnixSeconds(benchmark::State& state) {
  145. const absl::Time t = absl::UnixEpoch() + absl::Seconds(123);
  146. while (state.KeepRunning()) {
  147. benchmark::DoNotOptimize(absl::ToUnixSeconds(t));
  148. }
  149. }
  150. BENCHMARK(BM_Time_ToUnixSeconds);
  151. //
  152. // FromCivil
  153. //
  154. // In each "FromCivil" benchmark we switch between two YMDhms values
  155. // separated by at least one transition in order to defeat any internal
  156. // caching of previous results (e.g., see time_local_hint_).
  157. //
  158. // The "UTC" variants use UTC instead of the Google/local time zone.
  159. // The "Day0" variants require normalization of the day of month.
  160. //
  161. void BM_Time_FromCivil_Absl(benchmark::State& state) {
  162. const absl::TimeZone tz =
  163. absl::time_internal::LoadTimeZone("America/Los_Angeles");
  164. int i = 0;
  165. while (state.KeepRunning()) {
  166. if ((i & 1) == 0) {
  167. absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz);
  168. } else {
  169. absl::FromCivil(absl::CivilSecond(2013, 11, 15, 18, 30, 27), tz);
  170. }
  171. ++i;
  172. }
  173. }
  174. BENCHMARK(BM_Time_FromCivil_Absl);
  175. void BM_Time_FromCivil_Libc(benchmark::State& state) {
  176. // No timezone support, so just use localtime.
  177. int i = 0;
  178. while (state.KeepRunning()) {
  179. struct tm tm;
  180. if ((i & 1) == 0) {
  181. tm.tm_year = 2014 - 1900;
  182. tm.tm_mon = 12 - 1;
  183. tm.tm_mday = 18;
  184. tm.tm_hour = 20;
  185. tm.tm_min = 16;
  186. tm.tm_sec = 18;
  187. } else {
  188. tm.tm_year = 2013 - 1900;
  189. tm.tm_mon = 11 - 1;
  190. tm.tm_mday = 15;
  191. tm.tm_hour = 18;
  192. tm.tm_min = 30;
  193. tm.tm_sec = 27;
  194. }
  195. tm.tm_isdst = -1;
  196. mktime(&tm);
  197. ++i;
  198. }
  199. }
  200. BENCHMARK(BM_Time_FromCivil_Libc);
  201. void BM_Time_FromCivilUTC_Absl(benchmark::State& state) {
  202. const absl::TimeZone tz = absl::UTCTimeZone();
  203. while (state.KeepRunning()) {
  204. absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz);
  205. }
  206. }
  207. BENCHMARK(BM_Time_FromCivilUTC_Absl);
  208. void BM_Time_FromCivilDay0_Absl(benchmark::State& state) {
  209. const absl::TimeZone tz =
  210. absl::time_internal::LoadTimeZone("America/Los_Angeles");
  211. int i = 0;
  212. while (state.KeepRunning()) {
  213. if ((i & 1) == 0) {
  214. absl::FromCivil(absl::CivilSecond(2014, 12, 0, 20, 16, 18), tz);
  215. } else {
  216. absl::FromCivil(absl::CivilSecond(2013, 11, 0, 18, 30, 27), tz);
  217. }
  218. ++i;
  219. }
  220. }
  221. BENCHMARK(BM_Time_FromCivilDay0_Absl);
  222. void BM_Time_FromCivilDay0_Libc(benchmark::State& state) {
  223. // No timezone support, so just use localtime.
  224. int i = 0;
  225. while (state.KeepRunning()) {
  226. struct tm tm;
  227. if ((i & 1) == 0) {
  228. tm.tm_year = 2014 - 1900;
  229. tm.tm_mon = 12 - 1;
  230. tm.tm_mday = 0;
  231. tm.tm_hour = 20;
  232. tm.tm_min = 16;
  233. tm.tm_sec = 18;
  234. } else {
  235. tm.tm_year = 2013 - 1900;
  236. tm.tm_mon = 11 - 1;
  237. tm.tm_mday = 0;
  238. tm.tm_hour = 18;
  239. tm.tm_min = 30;
  240. tm.tm_sec = 27;
  241. }
  242. tm.tm_isdst = -1;
  243. mktime(&tm);
  244. ++i;
  245. }
  246. }
  247. BENCHMARK(BM_Time_FromCivilDay0_Libc);
  248. //
  249. // To/FromTimespec
  250. //
  251. void BM_Time_ToTimespec(benchmark::State& state) {
  252. absl::Time now = absl::Now();
  253. while (state.KeepRunning()) {
  254. benchmark::DoNotOptimize(absl::ToTimespec(now));
  255. }
  256. }
  257. BENCHMARK(BM_Time_ToTimespec);
  258. void BM_Time_FromTimespec(benchmark::State& state) {
  259. timespec ts = absl::ToTimespec(absl::Now());
  260. while (state.KeepRunning()) {
  261. if (++ts.tv_nsec == 1000 * 1000 * 1000) {
  262. ++ts.tv_sec;
  263. ts.tv_nsec = 0;
  264. }
  265. benchmark::DoNotOptimize(absl::TimeFromTimespec(ts));
  266. }
  267. }
  268. BENCHMARK(BM_Time_FromTimespec);
  269. //
  270. // Comparison with InfiniteFuture/Past
  271. //
  272. void BM_Time_InfiniteFuture(benchmark::State& state) {
  273. while (state.KeepRunning()) {
  274. benchmark::DoNotOptimize(absl::InfiniteFuture());
  275. }
  276. }
  277. BENCHMARK(BM_Time_InfiniteFuture);
  278. void BM_Time_InfinitePast(benchmark::State& state) {
  279. while (state.KeepRunning()) {
  280. benchmark::DoNotOptimize(absl::InfinitePast());
  281. }
  282. }
  283. BENCHMARK(BM_Time_InfinitePast);
  284. } // namespace