util_smartptr.bench.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include <memory>
  10. #include "benchmark/benchmark_api.h"
  11. static void BM_SharedPtrCreateDestroy(benchmark::State& st) {
  12. while (st.KeepRunning()) {
  13. auto sp = std::make_shared<int>(42);
  14. benchmark::DoNotOptimize(sp.get());
  15. }
  16. }
  17. BENCHMARK(BM_SharedPtrCreateDestroy);
  18. static void BM_SharedPtrIncDecRef(benchmark::State& st) {
  19. auto sp = std::make_shared<int>(42);
  20. benchmark::DoNotOptimize(sp.get());
  21. while (st.KeepRunning()) {
  22. std::shared_ptr<int> sp2(sp);
  23. benchmark::ClobberMemory();
  24. }
  25. }
  26. BENCHMARK(BM_SharedPtrIncDecRef);
  27. static void BM_WeakPtrIncDecRef(benchmark::State& st) {
  28. auto sp = std::make_shared<int>(42);
  29. benchmark::DoNotOptimize(sp.get());
  30. while (st.KeepRunning()) {
  31. std::weak_ptr<int> wp(sp);
  32. benchmark::ClobberMemory();
  33. }
  34. }
  35. BENCHMARK(BM_WeakPtrIncDecRef);
  36. BENCHMARK_MAIN()