main.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. void BM_gmtime_r_Random(benchmark::State& state, TDuration window) {
  30. time_t now = TInstant::Now().TimeT();
  31. struct tm buf {};
  32. TFastRng<ui32> rng(2);
  33. const size_t range = window.Seconds();
  34. for (auto _ : state) {
  35. size_t offset = rng.GenRand() % range;
  36. time_t v = now - offset;
  37. Y_DO_NOT_OPTIMIZE_AWAY(gmtime_r(&v, &buf));
  38. }
  39. }
  40. BENCHMARK(BM_GmTimeR);
  41. BENCHMARK(BM_gmtime_r);
  42. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_hour, TDuration::Hours(1));
  43. BENCHMARK_CAPTURE(BM_gmtime_r_Random, last_hour, TDuration::Hours(1));
  44. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_day, TDuration::Days(1));
  45. BENCHMARK_CAPTURE(BM_gmtime_r_Random, last_day, TDuration::Days(1));
  46. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_month, TDuration::Days(31));
  47. BENCHMARK_CAPTURE(BM_gmtime_r_Random, last_month, TDuration::Days(31));
  48. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_year, TDuration::Days(365));
  49. BENCHMARK_CAPTURE(BM_gmtime_r_Random, last_year, TDuration::Days(365));
  50. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_decade, TDuration::Days(3653));
  51. BENCHMARK_CAPTURE(BM_gmtime_r_Random, last_decade, TDuration::Days(3653));
  52. BENCHMARK_CAPTURE(BM_GmTimeRRandom, last_half_centry, TDuration::Days(18262));
  53. BENCHMARK_CAPTURE(BM_gmtime_r_Random, last_half_century, TDuration::Days(18262));