Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <cmath>
00015 #include <cstddef>
00016 #include <cstdint>
00017 #include <ctime>
00018 #include <string>
00019
00020 #include "absl/base/attributes.h"
00021 #include "absl/time/time.h"
00022 #include "benchmark/benchmark.h"
00023
00024 namespace {
00025
00026
00027
00028
00029
00030 void BM_Duration_Factory_Nanoseconds(benchmark::State& state) {
00031 int64_t i = 0;
00032 while (state.KeepRunning()) {
00033 benchmark::DoNotOptimize(absl::Nanoseconds(i));
00034 i += 314159;
00035 }
00036 }
00037 BENCHMARK(BM_Duration_Factory_Nanoseconds);
00038
00039 void BM_Duration_Factory_Microseconds(benchmark::State& state) {
00040 int64_t i = 0;
00041 while (state.KeepRunning()) {
00042 benchmark::DoNotOptimize(absl::Microseconds(i));
00043 i += 314;
00044 }
00045 }
00046 BENCHMARK(BM_Duration_Factory_Microseconds);
00047
00048 void BM_Duration_Factory_Milliseconds(benchmark::State& state) {
00049 int64_t i = 0;
00050 while (state.KeepRunning()) {
00051 benchmark::DoNotOptimize(absl::Milliseconds(i));
00052 i += 1;
00053 }
00054 }
00055 BENCHMARK(BM_Duration_Factory_Milliseconds);
00056
00057 void BM_Duration_Factory_Seconds(benchmark::State& state) {
00058 int64_t i = 0;
00059 while (state.KeepRunning()) {
00060 benchmark::DoNotOptimize(absl::Seconds(i));
00061 i += 1;
00062 }
00063 }
00064 BENCHMARK(BM_Duration_Factory_Seconds);
00065
00066 void BM_Duration_Factory_Minutes(benchmark::State& state) {
00067 int64_t i = 0;
00068 while (state.KeepRunning()) {
00069 benchmark::DoNotOptimize(absl::Minutes(i));
00070 i += 1;
00071 }
00072 }
00073 BENCHMARK(BM_Duration_Factory_Minutes);
00074
00075 void BM_Duration_Factory_Hours(benchmark::State& state) {
00076 int64_t i = 0;
00077 while (state.KeepRunning()) {
00078 benchmark::DoNotOptimize(absl::Hours(i));
00079 i += 1;
00080 }
00081 }
00082 BENCHMARK(BM_Duration_Factory_Hours);
00083
00084 void BM_Duration_Factory_DoubleNanoseconds(benchmark::State& state) {
00085 double d = 1;
00086 while (state.KeepRunning()) {
00087 benchmark::DoNotOptimize(absl::Nanoseconds(d));
00088 d = d * 1.00000001 + 1;
00089 }
00090 }
00091 BENCHMARK(BM_Duration_Factory_DoubleNanoseconds);
00092
00093 void BM_Duration_Factory_DoubleMicroseconds(benchmark::State& state) {
00094 double d = 1e-3;
00095 while (state.KeepRunning()) {
00096 benchmark::DoNotOptimize(absl::Microseconds(d));
00097 d = d * 1.00000001 + 1e-3;
00098 }
00099 }
00100 BENCHMARK(BM_Duration_Factory_DoubleMicroseconds);
00101
00102 void BM_Duration_Factory_DoubleMilliseconds(benchmark::State& state) {
00103 double d = 1e-6;
00104 while (state.KeepRunning()) {
00105 benchmark::DoNotOptimize(absl::Milliseconds(d));
00106 d = d * 1.00000001 + 1e-6;
00107 }
00108 }
00109 BENCHMARK(BM_Duration_Factory_DoubleMilliseconds);
00110
00111 void BM_Duration_Factory_DoubleSeconds(benchmark::State& state) {
00112 double d = 1e-9;
00113 while (state.KeepRunning()) {
00114 benchmark::DoNotOptimize(absl::Seconds(d));
00115 d = d * 1.00000001 + 1e-9;
00116 }
00117 }
00118 BENCHMARK(BM_Duration_Factory_DoubleSeconds);
00119
00120 void BM_Duration_Factory_DoubleMinutes(benchmark::State& state) {
00121 double d = 1e-9;
00122 while (state.KeepRunning()) {
00123 benchmark::DoNotOptimize(absl::Minutes(d));
00124 d = d * 1.00000001 + 1e-9;
00125 }
00126 }
00127 BENCHMARK(BM_Duration_Factory_DoubleMinutes);
00128
00129 void BM_Duration_Factory_DoubleHours(benchmark::State& state) {
00130 double d = 1e-9;
00131 while (state.KeepRunning()) {
00132 benchmark::DoNotOptimize(absl::Hours(d));
00133 d = d * 1.00000001 + 1e-9;
00134 }
00135 }
00136 BENCHMARK(BM_Duration_Factory_DoubleHours);
00137
00138
00139
00140
00141
00142 void BM_Duration_Addition(benchmark::State& state) {
00143 absl::Duration d = absl::Nanoseconds(1);
00144 absl::Duration step = absl::Milliseconds(1);
00145 while (state.KeepRunning()) {
00146 benchmark::DoNotOptimize(d += step);
00147 }
00148 }
00149 BENCHMARK(BM_Duration_Addition);
00150
00151 void BM_Duration_Subtraction(benchmark::State& state) {
00152 absl::Duration d = absl::Seconds(std::numeric_limits<int64_t>::max());
00153 absl::Duration step = absl::Milliseconds(1);
00154 while (state.KeepRunning()) {
00155 benchmark::DoNotOptimize(d -= step);
00156 }
00157 }
00158 BENCHMARK(BM_Duration_Subtraction);
00159
00160 void BM_Duration_Multiplication_Fixed(benchmark::State& state) {
00161 absl::Duration d = absl::Milliseconds(1);
00162 absl::Duration s;
00163 int i = 0;
00164 while (state.KeepRunning()) {
00165 benchmark::DoNotOptimize(s += d * (i + 1));
00166 ++i;
00167 }
00168 }
00169 BENCHMARK(BM_Duration_Multiplication_Fixed);
00170
00171 void BM_Duration_Multiplication_Double(benchmark::State& state) {
00172 absl::Duration d = absl::Milliseconds(1);
00173 absl::Duration s;
00174 int i = 0;
00175 while (state.KeepRunning()) {
00176 benchmark::DoNotOptimize(s += d * (i + 1.0));
00177 ++i;
00178 }
00179 }
00180 BENCHMARK(BM_Duration_Multiplication_Double);
00181
00182 void BM_Duration_Division_Fixed(benchmark::State& state) {
00183 absl::Duration d = absl::Seconds(1);
00184 int i = 0;
00185 while (state.KeepRunning()) {
00186 benchmark::DoNotOptimize(d /= i + 1);
00187 ++i;
00188 }
00189 }
00190 BENCHMARK(BM_Duration_Division_Fixed);
00191
00192 void BM_Duration_Division_Double(benchmark::State& state) {
00193 absl::Duration d = absl::Seconds(1);
00194 int i = 0;
00195 while (state.KeepRunning()) {
00196 benchmark::DoNotOptimize(d /= i + 1.0);
00197 ++i;
00198 }
00199 }
00200 BENCHMARK(BM_Duration_Division_Double);
00201
00202 void BM_Duration_FDivDuration_Nanoseconds(benchmark::State& state) {
00203 double d = 1;
00204 int i = 0;
00205 while (state.KeepRunning()) {
00206 benchmark::DoNotOptimize(
00207 d += absl::FDivDuration(absl::Milliseconds(i), absl::Nanoseconds(1)));
00208 ++i;
00209 }
00210 }
00211 BENCHMARK(BM_Duration_FDivDuration_Nanoseconds);
00212
00213 void BM_Duration_IDivDuration_Nanoseconds(benchmark::State& state) {
00214 int64_t a = 1;
00215 absl::Duration ignore;
00216 int i = 0;
00217 while (state.KeepRunning()) {
00218 benchmark::DoNotOptimize(a +=
00219 absl::IDivDuration(absl::Nanoseconds(i),
00220 absl::Nanoseconds(1), &ignore));
00221 ++i;
00222 }
00223 }
00224 BENCHMARK(BM_Duration_IDivDuration_Nanoseconds);
00225
00226 void BM_Duration_IDivDuration_Microseconds(benchmark::State& state) {
00227 int64_t a = 1;
00228 absl::Duration ignore;
00229 int i = 0;
00230 while (state.KeepRunning()) {
00231 benchmark::DoNotOptimize(a += absl::IDivDuration(absl::Microseconds(i),
00232 absl::Microseconds(1),
00233 &ignore));
00234 ++i;
00235 }
00236 }
00237 BENCHMARK(BM_Duration_IDivDuration_Microseconds);
00238
00239 void BM_Duration_IDivDuration_Milliseconds(benchmark::State& state) {
00240 int64_t a = 1;
00241 absl::Duration ignore;
00242 int i = 0;
00243 while (state.KeepRunning()) {
00244 benchmark::DoNotOptimize(a += absl::IDivDuration(absl::Milliseconds(i),
00245 absl::Milliseconds(1),
00246 &ignore));
00247 ++i;
00248 }
00249 }
00250 BENCHMARK(BM_Duration_IDivDuration_Milliseconds);
00251
00252 void BM_Duration_IDivDuration_Seconds(benchmark::State& state) {
00253 int64_t a = 1;
00254 absl::Duration ignore;
00255 int i = 0;
00256 while (state.KeepRunning()) {
00257 benchmark::DoNotOptimize(
00258 a += absl::IDivDuration(absl::Seconds(i), absl::Seconds(1), &ignore));
00259 ++i;
00260 }
00261 }
00262 BENCHMARK(BM_Duration_IDivDuration_Seconds);
00263
00264 void BM_Duration_IDivDuration_Minutes(benchmark::State& state) {
00265 int64_t a = 1;
00266 absl::Duration ignore;
00267 int i = 0;
00268 while (state.KeepRunning()) {
00269 benchmark::DoNotOptimize(
00270 a += absl::IDivDuration(absl::Minutes(i), absl::Minutes(1), &ignore));
00271 ++i;
00272 }
00273 }
00274 BENCHMARK(BM_Duration_IDivDuration_Minutes);
00275
00276 void BM_Duration_IDivDuration_Hours(benchmark::State& state) {
00277 int64_t a = 1;
00278 absl::Duration ignore;
00279 int i = 0;
00280 while (state.KeepRunning()) {
00281 benchmark::DoNotOptimize(
00282 a += absl::IDivDuration(absl::Hours(i), absl::Hours(1), &ignore));
00283 ++i;
00284 }
00285 }
00286 BENCHMARK(BM_Duration_IDivDuration_Hours);
00287
00288 void BM_Duration_ToInt64Nanoseconds(benchmark::State& state) {
00289 absl::Duration d = absl::Seconds(100000);
00290 while (state.KeepRunning()) {
00291 benchmark::DoNotOptimize(absl::ToInt64Nanoseconds(d));
00292 }
00293 }
00294 BENCHMARK(BM_Duration_ToInt64Nanoseconds);
00295
00296 void BM_Duration_ToInt64Microseconds(benchmark::State& state) {
00297 absl::Duration d = absl::Seconds(100000);
00298 while (state.KeepRunning()) {
00299 benchmark::DoNotOptimize(absl::ToInt64Microseconds(d));
00300 }
00301 }
00302 BENCHMARK(BM_Duration_ToInt64Microseconds);
00303
00304 void BM_Duration_ToInt64Milliseconds(benchmark::State& state) {
00305 absl::Duration d = absl::Seconds(100000);
00306 while (state.KeepRunning()) {
00307 benchmark::DoNotOptimize(absl::ToInt64Milliseconds(d));
00308 }
00309 }
00310 BENCHMARK(BM_Duration_ToInt64Milliseconds);
00311
00312 void BM_Duration_ToInt64Seconds(benchmark::State& state) {
00313 absl::Duration d = absl::Seconds(100000);
00314 while (state.KeepRunning()) {
00315 benchmark::DoNotOptimize(absl::ToInt64Seconds(d));
00316 }
00317 }
00318 BENCHMARK(BM_Duration_ToInt64Seconds);
00319
00320 void BM_Duration_ToInt64Minutes(benchmark::State& state) {
00321 absl::Duration d = absl::Seconds(100000);
00322 while (state.KeepRunning()) {
00323 benchmark::DoNotOptimize(absl::ToInt64Minutes(d));
00324 }
00325 }
00326 BENCHMARK(BM_Duration_ToInt64Minutes);
00327
00328 void BM_Duration_ToInt64Hours(benchmark::State& state) {
00329 absl::Duration d = absl::Seconds(100000);
00330 while (state.KeepRunning()) {
00331 benchmark::DoNotOptimize(absl::ToInt64Hours(d));
00332 }
00333 }
00334 BENCHMARK(BM_Duration_ToInt64Hours);
00335
00336
00337
00338
00339
00340 void BM_Duration_ToTimespec_AbslTime(benchmark::State& state) {
00341 absl::Duration d = absl::Seconds(1);
00342 while (state.KeepRunning()) {
00343 benchmark::DoNotOptimize(absl::ToTimespec(d));
00344 }
00345 }
00346 BENCHMARK(BM_Duration_ToTimespec_AbslTime);
00347
00348 ABSL_ATTRIBUTE_NOINLINE timespec DoubleToTimespec(double seconds) {
00349 timespec ts;
00350 ts.tv_sec = seconds;
00351 ts.tv_nsec = (seconds - ts.tv_sec) * (1000 * 1000 * 1000);
00352 return ts;
00353 }
00354
00355 void BM_Duration_ToTimespec_Double(benchmark::State& state) {
00356 while (state.KeepRunning()) {
00357 benchmark::DoNotOptimize(DoubleToTimespec(1.0));
00358 }
00359 }
00360 BENCHMARK(BM_Duration_ToTimespec_Double);
00361
00362 void BM_Duration_FromTimespec_AbslTime(benchmark::State& state) {
00363 timespec ts;
00364 ts.tv_sec = 0;
00365 ts.tv_nsec = 0;
00366 while (state.KeepRunning()) {
00367 if (++ts.tv_nsec == 1000 * 1000 * 1000) {
00368 ++ts.tv_sec;
00369 ts.tv_nsec = 0;
00370 }
00371 benchmark::DoNotOptimize(absl::DurationFromTimespec(ts));
00372 }
00373 }
00374 BENCHMARK(BM_Duration_FromTimespec_AbslTime);
00375
00376 ABSL_ATTRIBUTE_NOINLINE double TimespecToDouble(timespec ts) {
00377 return ts.tv_sec + (ts.tv_nsec / (1000 * 1000 * 1000));
00378 }
00379
00380 void BM_Duration_FromTimespec_Double(benchmark::State& state) {
00381 timespec ts;
00382 ts.tv_sec = 0;
00383 ts.tv_nsec = 0;
00384 while (state.KeepRunning()) {
00385 if (++ts.tv_nsec == 1000 * 1000 * 1000) {
00386 ++ts.tv_sec;
00387 ts.tv_nsec = 0;
00388 }
00389 benchmark::DoNotOptimize(TimespecToDouble(ts));
00390 }
00391 }
00392 BENCHMARK(BM_Duration_FromTimespec_Double);
00393
00394
00395
00396
00397
00398 const char* const kDurations[] = {
00399 "0",
00400 "123ns",
00401 "1h2m3s",
00402 "-2h3m4.005006007s",
00403 "2562047788015215h30m7.99999999975s",
00404 };
00405 const int kNumDurations = sizeof(kDurations) / sizeof(kDurations[0]);
00406
00407 void BM_Duration_FormatDuration(benchmark::State& state) {
00408 const std::string s = kDurations[state.range(0)];
00409 state.SetLabel(s);
00410 absl::Duration d;
00411 absl::ParseDuration(kDurations[state.range(0)], &d);
00412 while (state.KeepRunning()) {
00413 benchmark::DoNotOptimize(absl::FormatDuration(d));
00414 }
00415 }
00416 BENCHMARK(BM_Duration_FormatDuration)->DenseRange(0, kNumDurations - 1);
00417
00418 void BM_Duration_ParseDuration(benchmark::State& state) {
00419 const std::string s = kDurations[state.range(0)];
00420 state.SetLabel(s);
00421 absl::Duration d;
00422 while (state.KeepRunning()) {
00423 benchmark::DoNotOptimize(absl::ParseDuration(s, &d));
00424 }
00425 }
00426 BENCHMARK(BM_Duration_ParseDuration)->DenseRange(0, kNumDurations - 1);
00427
00428 }