main.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <library/cpp/testing/gbenchmark/benchmark.h>
  2. #include <util/datetime/base.h>
  3. #include <util/random/fast.h>
  4. void BM_GmTimeR(benchmark::State& state) {
  5. time_t now = TInstant::Now().TimeT();
  6. struct tm buf {};
  7. for (auto _ : state) {
  8. Y_DO_NOT_OPTIMIZE_AWAY(GmTimeR(&now, &buf));
  9. }
  10. }
  11. void BM_gmtime_r(benchmark::State& state) {
  12. time_t now = TInstant::Now().TimeT();
  13. struct tm buf {};
  14. for (auto _ : state) {
  15. Y_DO_NOT_OPTIMIZE_AWAY(gmtime_r(&now, &buf));
  16. }
  17. }
  18. void BM_GmTimeRRandom(benchmark::State& state, TDuration window) {
  19. time_t now = TInstant::Now().TimeT();
  20. struct tm buf {};
  21. TFastRng<ui32> rng(2);
  22. const size_t range = window.Seconds();
  23. for (auto _ : state) {
  24. size_t offset = rng.GenRand() % range;
  25. time_t v = now - offset;
  26. Y_DO_NOT_OPTIMIZE_AWAY(GmTimeR(&v, &buf));
  27. }
  28. }
  29. BENCHMARK(BM_GmTimeR);
  30. BENCHMARK(BM_gmtime_r);
  31. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_hour, TDuration::Hours(1));
  32. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_day, TDuration::Days(1));
  33. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_mount, TDuration::Days(31));
  34. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_year, TDuration::Days(365));
  35. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_decade, TDuration::Days(3653));
  36. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_half_centry, TDuration::Days(18262));