util_smartptr.bench.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include <memory>
  9. #include "benchmark/benchmark.h"
  10. static void BM_SharedPtrCreateDestroy(benchmark::State& st) {
  11. while (st.KeepRunning()) {
  12. auto sp = std::make_shared<int>(42);
  13. benchmark::DoNotOptimize(sp.get());
  14. }
  15. }
  16. BENCHMARK(BM_SharedPtrCreateDestroy);
  17. static void BM_SharedPtrIncDecRef(benchmark::State& st) {
  18. auto sp = std::make_shared<int>(42);
  19. benchmark::DoNotOptimize(sp.get());
  20. while (st.KeepRunning()) {
  21. std::shared_ptr<int> sp2(sp);
  22. benchmark::ClobberMemory();
  23. }
  24. }
  25. BENCHMARK(BM_SharedPtrIncDecRef);
  26. static void BM_WeakPtrIncDecRef(benchmark::State& st) {
  27. auto sp = std::make_shared<int>(42);
  28. benchmark::DoNotOptimize(sp.get());
  29. while (st.KeepRunning()) {
  30. std::weak_ptr<int> wp(sp);
  31. benchmark::ClobberMemory();
  32. }
  33. }
  34. BENCHMARK(BM_WeakPtrIncDecRef);
  35. BENCHMARK_MAIN();