duration_benchmark.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 <cmath>
  14. #include <cstddef>
  15. #include <cstdint>
  16. #include <ctime>
  17. #include <string>
  18. #include "absl/base/attributes.h"
  19. #include "absl/flags/flag.h"
  20. #include "absl/time/time.h"
  21. #include "benchmark/benchmark.h"
  22. ABSL_FLAG(absl::Duration, absl_duration_flag_for_benchmark,
  23. absl::Milliseconds(1),
  24. "Flag to use for benchmarking duration flag access speed.");
  25. namespace {
  26. //
  27. // Factory functions
  28. //
  29. void BM_Duration_Factory_Nanoseconds(benchmark::State& state) {
  30. int64_t i = 0;
  31. while (state.KeepRunning()) {
  32. benchmark::DoNotOptimize(absl::Nanoseconds(i));
  33. i += 314159;
  34. }
  35. }
  36. BENCHMARK(BM_Duration_Factory_Nanoseconds);
  37. void BM_Duration_Factory_Microseconds(benchmark::State& state) {
  38. int64_t i = 0;
  39. while (state.KeepRunning()) {
  40. benchmark::DoNotOptimize(absl::Microseconds(i));
  41. i += 314;
  42. }
  43. }
  44. BENCHMARK(BM_Duration_Factory_Microseconds);
  45. void BM_Duration_Factory_Milliseconds(benchmark::State& state) {
  46. int64_t i = 0;
  47. while (state.KeepRunning()) {
  48. benchmark::DoNotOptimize(absl::Milliseconds(i));
  49. i += 1;
  50. }
  51. }
  52. BENCHMARK(BM_Duration_Factory_Milliseconds);
  53. void BM_Duration_Factory_Seconds(benchmark::State& state) {
  54. int64_t i = 0;
  55. while (state.KeepRunning()) {
  56. benchmark::DoNotOptimize(absl::Seconds(i));
  57. i += 1;
  58. }
  59. }
  60. BENCHMARK(BM_Duration_Factory_Seconds);
  61. void BM_Duration_Factory_Minutes(benchmark::State& state) {
  62. int64_t i = 0;
  63. while (state.KeepRunning()) {
  64. benchmark::DoNotOptimize(absl::Minutes(i));
  65. i += 1;
  66. }
  67. }
  68. BENCHMARK(BM_Duration_Factory_Minutes);
  69. void BM_Duration_Factory_Hours(benchmark::State& state) {
  70. int64_t i = 0;
  71. while (state.KeepRunning()) {
  72. benchmark::DoNotOptimize(absl::Hours(i));
  73. i += 1;
  74. }
  75. }
  76. BENCHMARK(BM_Duration_Factory_Hours);
  77. void BM_Duration_Factory_DoubleNanoseconds(benchmark::State& state) {
  78. double d = 1;
  79. while (state.KeepRunning()) {
  80. benchmark::DoNotOptimize(absl::Nanoseconds(d));
  81. d = d * 1.00000001 + 1;
  82. }
  83. }
  84. BENCHMARK(BM_Duration_Factory_DoubleNanoseconds);
  85. void BM_Duration_Factory_DoubleMicroseconds(benchmark::State& state) {
  86. double d = 1e-3;
  87. while (state.KeepRunning()) {
  88. benchmark::DoNotOptimize(absl::Microseconds(d));
  89. d = d * 1.00000001 + 1e-3;
  90. }
  91. }
  92. BENCHMARK(BM_Duration_Factory_DoubleMicroseconds);
  93. void BM_Duration_Factory_DoubleMilliseconds(benchmark::State& state) {
  94. double d = 1e-6;
  95. while (state.KeepRunning()) {
  96. benchmark::DoNotOptimize(absl::Milliseconds(d));
  97. d = d * 1.00000001 + 1e-6;
  98. }
  99. }
  100. BENCHMARK(BM_Duration_Factory_DoubleMilliseconds);
  101. void BM_Duration_Factory_DoubleSeconds(benchmark::State& state) {
  102. double d = 1e-9;
  103. while (state.KeepRunning()) {
  104. benchmark::DoNotOptimize(absl::Seconds(d));
  105. d = d * 1.00000001 + 1e-9;
  106. }
  107. }
  108. BENCHMARK(BM_Duration_Factory_DoubleSeconds);
  109. void BM_Duration_Factory_DoubleMinutes(benchmark::State& state) {
  110. double d = 1e-9;
  111. while (state.KeepRunning()) {
  112. benchmark::DoNotOptimize(absl::Minutes(d));
  113. d = d * 1.00000001 + 1e-9;
  114. }
  115. }
  116. BENCHMARK(BM_Duration_Factory_DoubleMinutes);
  117. void BM_Duration_Factory_DoubleHours(benchmark::State& state) {
  118. double d = 1e-9;
  119. while (state.KeepRunning()) {
  120. benchmark::DoNotOptimize(absl::Hours(d));
  121. d = d * 1.00000001 + 1e-9;
  122. }
  123. }
  124. BENCHMARK(BM_Duration_Factory_DoubleHours);
  125. //
  126. // Arithmetic
  127. //
  128. void BM_Duration_Addition(benchmark::State& state) {
  129. absl::Duration d = absl::Nanoseconds(1);
  130. absl::Duration step = absl::Milliseconds(1);
  131. while (state.KeepRunning()) {
  132. benchmark::DoNotOptimize(d += step);
  133. }
  134. }
  135. BENCHMARK(BM_Duration_Addition);
  136. void BM_Duration_Subtraction(benchmark::State& state) {
  137. absl::Duration d = absl::Seconds(std::numeric_limits<int64_t>::max());
  138. absl::Duration step = absl::Milliseconds(1);
  139. while (state.KeepRunning()) {
  140. benchmark::DoNotOptimize(d -= step);
  141. }
  142. }
  143. BENCHMARK(BM_Duration_Subtraction);
  144. void BM_Duration_Multiplication_Fixed(benchmark::State& state) {
  145. absl::Duration d = absl::Milliseconds(1);
  146. absl::Duration s;
  147. int i = 0;
  148. while (state.KeepRunning()) {
  149. benchmark::DoNotOptimize(s += d * (i + 1));
  150. ++i;
  151. }
  152. }
  153. BENCHMARK(BM_Duration_Multiplication_Fixed);
  154. void BM_Duration_Multiplication_Double(benchmark::State& state) {
  155. absl::Duration d = absl::Milliseconds(1);
  156. absl::Duration s;
  157. int i = 0;
  158. while (state.KeepRunning()) {
  159. benchmark::DoNotOptimize(s += d * (i + 1.0));
  160. ++i;
  161. }
  162. }
  163. BENCHMARK(BM_Duration_Multiplication_Double);
  164. void BM_Duration_Division_Fixed(benchmark::State& state) {
  165. absl::Duration d = absl::Seconds(1);
  166. int i = 0;
  167. while (state.KeepRunning()) {
  168. benchmark::DoNotOptimize(d /= i + 1);
  169. ++i;
  170. }
  171. }
  172. BENCHMARK(BM_Duration_Division_Fixed);
  173. void BM_Duration_Division_Double(benchmark::State& state) {
  174. absl::Duration d = absl::Seconds(1);
  175. int i = 0;
  176. while (state.KeepRunning()) {
  177. benchmark::DoNotOptimize(d /= i + 1.0);
  178. ++i;
  179. }
  180. }
  181. BENCHMARK(BM_Duration_Division_Double);
  182. void BM_Duration_FDivDuration_Nanoseconds(benchmark::State& state) {
  183. double d = 1;
  184. int i = 0;
  185. while (state.KeepRunning()) {
  186. benchmark::DoNotOptimize(
  187. d += absl::FDivDuration(absl::Milliseconds(i), absl::Nanoseconds(1)));
  188. ++i;
  189. }
  190. }
  191. BENCHMARK(BM_Duration_FDivDuration_Nanoseconds);
  192. void BM_Duration_IDivDuration_Nanoseconds(benchmark::State& state) {
  193. int64_t a = 1;
  194. absl::Duration ignore;
  195. int i = 0;
  196. while (state.KeepRunning()) {
  197. benchmark::DoNotOptimize(a +=
  198. absl::IDivDuration(absl::Nanoseconds(i),
  199. absl::Nanoseconds(1), &ignore));
  200. ++i;
  201. }
  202. }
  203. BENCHMARK(BM_Duration_IDivDuration_Nanoseconds);
  204. void BM_Duration_IDivDuration_Microseconds(benchmark::State& state) {
  205. int64_t a = 1;
  206. absl::Duration ignore;
  207. int i = 0;
  208. while (state.KeepRunning()) {
  209. benchmark::DoNotOptimize(a += absl::IDivDuration(absl::Microseconds(i),
  210. absl::Microseconds(1),
  211. &ignore));
  212. ++i;
  213. }
  214. }
  215. BENCHMARK(BM_Duration_IDivDuration_Microseconds);
  216. void BM_Duration_IDivDuration_Milliseconds(benchmark::State& state) {
  217. int64_t a = 1;
  218. absl::Duration ignore;
  219. int i = 0;
  220. while (state.KeepRunning()) {
  221. benchmark::DoNotOptimize(a += absl::IDivDuration(absl::Milliseconds(i),
  222. absl::Milliseconds(1),
  223. &ignore));
  224. ++i;
  225. }
  226. }
  227. BENCHMARK(BM_Duration_IDivDuration_Milliseconds);
  228. void BM_Duration_IDivDuration_Seconds(benchmark::State& state) {
  229. int64_t a = 1;
  230. absl::Duration ignore;
  231. int i = 0;
  232. while (state.KeepRunning()) {
  233. benchmark::DoNotOptimize(
  234. a += absl::IDivDuration(absl::Seconds(i), absl::Seconds(1), &ignore));
  235. ++i;
  236. }
  237. }
  238. BENCHMARK(BM_Duration_IDivDuration_Seconds);
  239. void BM_Duration_IDivDuration_Minutes(benchmark::State& state) {
  240. int64_t a = 1;
  241. absl::Duration ignore;
  242. int i = 0;
  243. while (state.KeepRunning()) {
  244. benchmark::DoNotOptimize(
  245. a += absl::IDivDuration(absl::Minutes(i), absl::Minutes(1), &ignore));
  246. ++i;
  247. }
  248. }
  249. BENCHMARK(BM_Duration_IDivDuration_Minutes);
  250. void BM_Duration_IDivDuration_Hours(benchmark::State& state) {
  251. int64_t a = 1;
  252. absl::Duration ignore;
  253. int i = 0;
  254. while (state.KeepRunning()) {
  255. benchmark::DoNotOptimize(
  256. a += absl::IDivDuration(absl::Hours(i), absl::Hours(1), &ignore));
  257. ++i;
  258. }
  259. }
  260. BENCHMARK(BM_Duration_IDivDuration_Hours);
  261. void BM_Duration_ToInt64Nanoseconds(benchmark::State& state) {
  262. absl::Duration d = absl::Seconds(100000);
  263. while (state.KeepRunning()) {
  264. benchmark::DoNotOptimize(absl::ToInt64Nanoseconds(d));
  265. }
  266. }
  267. BENCHMARK(BM_Duration_ToInt64Nanoseconds);
  268. void BM_Duration_ToInt64Microseconds(benchmark::State& state) {
  269. absl::Duration d = absl::Seconds(100000);
  270. while (state.KeepRunning()) {
  271. benchmark::DoNotOptimize(absl::ToInt64Microseconds(d));
  272. }
  273. }
  274. BENCHMARK(BM_Duration_ToInt64Microseconds);
  275. void BM_Duration_ToInt64Milliseconds(benchmark::State& state) {
  276. absl::Duration d = absl::Seconds(100000);
  277. while (state.KeepRunning()) {
  278. benchmark::DoNotOptimize(absl::ToInt64Milliseconds(d));
  279. }
  280. }
  281. BENCHMARK(BM_Duration_ToInt64Milliseconds);
  282. void BM_Duration_ToInt64Seconds(benchmark::State& state) {
  283. absl::Duration d = absl::Seconds(100000);
  284. while (state.KeepRunning()) {
  285. benchmark::DoNotOptimize(absl::ToInt64Seconds(d));
  286. }
  287. }
  288. BENCHMARK(BM_Duration_ToInt64Seconds);
  289. void BM_Duration_ToInt64Minutes(benchmark::State& state) {
  290. absl::Duration d = absl::Seconds(100000);
  291. while (state.KeepRunning()) {
  292. benchmark::DoNotOptimize(absl::ToInt64Minutes(d));
  293. }
  294. }
  295. BENCHMARK(BM_Duration_ToInt64Minutes);
  296. void BM_Duration_ToInt64Hours(benchmark::State& state) {
  297. absl::Duration d = absl::Seconds(100000);
  298. while (state.KeepRunning()) {
  299. benchmark::DoNotOptimize(absl::ToInt64Hours(d));
  300. }
  301. }
  302. BENCHMARK(BM_Duration_ToInt64Hours);
  303. //
  304. // To/FromTimespec
  305. //
  306. void BM_Duration_ToTimespec_AbslTime(benchmark::State& state) {
  307. absl::Duration d = absl::Seconds(1);
  308. while (state.KeepRunning()) {
  309. benchmark::DoNotOptimize(absl::ToTimespec(d));
  310. }
  311. }
  312. BENCHMARK(BM_Duration_ToTimespec_AbslTime);
  313. ABSL_ATTRIBUTE_NOINLINE timespec DoubleToTimespec(double seconds) {
  314. timespec ts;
  315. ts.tv_sec = seconds;
  316. ts.tv_nsec = (seconds - ts.tv_sec) * (1000 * 1000 * 1000);
  317. return ts;
  318. }
  319. void BM_Duration_ToTimespec_Double(benchmark::State& state) {
  320. while (state.KeepRunning()) {
  321. benchmark::DoNotOptimize(DoubleToTimespec(1.0));
  322. }
  323. }
  324. BENCHMARK(BM_Duration_ToTimespec_Double);
  325. void BM_Duration_FromTimespec_AbslTime(benchmark::State& state) {
  326. timespec ts;
  327. ts.tv_sec = 0;
  328. ts.tv_nsec = 0;
  329. while (state.KeepRunning()) {
  330. if (++ts.tv_nsec == 1000 * 1000 * 1000) {
  331. ++ts.tv_sec;
  332. ts.tv_nsec = 0;
  333. }
  334. benchmark::DoNotOptimize(absl::DurationFromTimespec(ts));
  335. }
  336. }
  337. BENCHMARK(BM_Duration_FromTimespec_AbslTime);
  338. ABSL_ATTRIBUTE_NOINLINE double TimespecToDouble(timespec ts) {
  339. return ts.tv_sec + (ts.tv_nsec / (1000 * 1000 * 1000));
  340. }
  341. void BM_Duration_FromTimespec_Double(benchmark::State& state) {
  342. timespec ts;
  343. ts.tv_sec = 0;
  344. ts.tv_nsec = 0;
  345. while (state.KeepRunning()) {
  346. if (++ts.tv_nsec == 1000 * 1000 * 1000) {
  347. ++ts.tv_sec;
  348. ts.tv_nsec = 0;
  349. }
  350. benchmark::DoNotOptimize(TimespecToDouble(ts));
  351. }
  352. }
  353. BENCHMARK(BM_Duration_FromTimespec_Double);
  354. //
  355. // String conversions
  356. //
  357. const char* const kDurations[] = {
  358. "0", // 0
  359. "123ns", // 1
  360. "1h2m3s", // 2
  361. "-2h3m4.005006007s", // 3
  362. "2562047788015215h30m7.99999999975s", // 4
  363. };
  364. const int kNumDurations = sizeof(kDurations) / sizeof(kDurations[0]);
  365. void BM_Duration_FormatDuration(benchmark::State& state) {
  366. const std::string s = kDurations[state.range(0)];
  367. state.SetLabel(s);
  368. absl::Duration d;
  369. absl::ParseDuration(kDurations[state.range(0)], &d);
  370. while (state.KeepRunning()) {
  371. benchmark::DoNotOptimize(absl::FormatDuration(d));
  372. }
  373. }
  374. BENCHMARK(BM_Duration_FormatDuration)->DenseRange(0, kNumDurations - 1);
  375. void BM_Duration_ParseDuration(benchmark::State& state) {
  376. const std::string s = kDurations[state.range(0)];
  377. state.SetLabel(s);
  378. absl::Duration d;
  379. while (state.KeepRunning()) {
  380. benchmark::DoNotOptimize(absl::ParseDuration(s, &d));
  381. }
  382. }
  383. BENCHMARK(BM_Duration_ParseDuration)->DenseRange(0, kNumDurations - 1);
  384. //
  385. // Flag access
  386. //
  387. void BM_Duration_GetFlag(benchmark::State& state) {
  388. while (state.KeepRunning()) {
  389. benchmark::DoNotOptimize(
  390. absl::GetFlag(FLAGS_absl_duration_flag_for_benchmark));
  391. }
  392. }
  393. BENCHMARK(BM_Duration_GetFlag);
  394. } // namespace