clock_benchmark.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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/clock.h"
  14. #if !defined(_WIN32)
  15. #include <sys/time.h>
  16. #else
  17. #include <winsock2.h>
  18. #endif // _WIN32
  19. #include <cstdio>
  20. #include "absl/base/internal/cycleclock.h"
  21. #include "benchmark/benchmark.h"
  22. namespace {
  23. void BM_Clock_Now_AbslTime(benchmark::State& state) {
  24. while (state.KeepRunning()) {
  25. benchmark::DoNotOptimize(absl::Now());
  26. }
  27. }
  28. BENCHMARK(BM_Clock_Now_AbslTime);
  29. void BM_Clock_Now_GetCurrentTimeNanos(benchmark::State& state) {
  30. while (state.KeepRunning()) {
  31. benchmark::DoNotOptimize(absl::GetCurrentTimeNanos());
  32. }
  33. }
  34. BENCHMARK(BM_Clock_Now_GetCurrentTimeNanos);
  35. void BM_Clock_Now_AbslTime_ToUnixNanos(benchmark::State& state) {
  36. while (state.KeepRunning()) {
  37. benchmark::DoNotOptimize(absl::ToUnixNanos(absl::Now()));
  38. }
  39. }
  40. BENCHMARK(BM_Clock_Now_AbslTime_ToUnixNanos);
  41. void BM_Clock_Now_CycleClock(benchmark::State& state) {
  42. while (state.KeepRunning()) {
  43. benchmark::DoNotOptimize(absl::base_internal::CycleClock::Now());
  44. }
  45. }
  46. BENCHMARK(BM_Clock_Now_CycleClock);
  47. #if !defined(_WIN32)
  48. static void BM_Clock_Now_gettimeofday(benchmark::State& state) {
  49. struct timeval tv;
  50. while (state.KeepRunning()) {
  51. benchmark::DoNotOptimize(gettimeofday(&tv, nullptr));
  52. }
  53. }
  54. BENCHMARK(BM_Clock_Now_gettimeofday);
  55. static void BM_Clock_Now_clock_gettime(benchmark::State& state) {
  56. struct timespec ts;
  57. while (state.KeepRunning()) {
  58. benchmark::DoNotOptimize(clock_gettime(CLOCK_REALTIME, &ts));
  59. }
  60. }
  61. BENCHMARK(BM_Clock_Now_clock_gettime);
  62. #endif // _WIN32
  63. } // namespace