File: intrusive_ptr_benchmark.cpp

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (93 lines) | stat: -rw-r--r-- 2,704 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <c10/util/intrusive_ptr.h>
#include <c10/util/irange.h>

#include <benchmark/benchmark.h>
#include <memory>

using c10::intrusive_ptr;
using c10::intrusive_ptr_target;
using c10::make_intrusive;
using c10::weak_intrusive_ptr;

namespace {

// Foo uses intrusive ptr
class Foo : public intrusive_ptr_target {
 public:
  Foo(int param_) : param(param_) {}
  int param;
};

class Bar : public std::enable_shared_from_this<Bar> {
 public:
  Bar(int param_) : param(param_) {}
  int param;
};

static void BM_IntrusivePtrCtorDtor(benchmark::State& state) {
  intrusive_ptr<Foo> var = make_intrusive<Foo>(0);
  while (state.KeepRunning()) {
    // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
    volatile intrusive_ptr<Foo> var2 = var;
  }
}
BENCHMARK(BM_IntrusivePtrCtorDtor);

static void BM_SharedPtrCtorDtor(benchmark::State& state) {
  std::shared_ptr<Bar> var = std::make_shared<Bar>(0);
  while (state.KeepRunning()) {
    // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
    volatile std::shared_ptr<Bar> var2 = var;
  }
}
BENCHMARK(BM_SharedPtrCtorDtor);

static void BM_IntrusivePtrArray(benchmark::State& state) {
  intrusive_ptr<Foo> var = make_intrusive<Foo>(0);
  const size_t kLength = state.range(0);
  std::vector<intrusive_ptr<Foo>> vararray(kLength);
  while (state.KeepRunning()) {
    for (const auto i : c10::irange(kLength)) {
      vararray[i] = var;
    }
    for (const auto i : c10::irange(kLength)) {
      vararray[i].reset();
    }
  }
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-avoid-magic-numbers)
BENCHMARK(BM_IntrusivePtrArray)->RangeMultiplier(2)->Range(16, 4096);

static void BM_SharedPtrArray(benchmark::State& state) {
  std::shared_ptr<Bar> var = std::make_shared<Bar>(0);
  const size_t kLength = state.range(0);
  std::vector<std::shared_ptr<Bar>> vararray(kLength);
  while (state.KeepRunning()) {
    for (const auto i : c10::irange(kLength)) {
      vararray[i] = var;
    }
    for (const auto i : c10::irange(kLength)) {
      vararray[i].reset();
    }
  }
}
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables,cppcoreguidelines-avoid-magic-numbers)
BENCHMARK(BM_SharedPtrArray)->RangeMultiplier(2)->Range(16, 4096);

static void BM_IntrusivePtrExclusiveOwnership(benchmark::State& state) {
  while (state.KeepRunning()) {
    volatile auto var = make_intrusive<Foo>(0);
  }
}
BENCHMARK(BM_IntrusivePtrExclusiveOwnership);

static void BM_SharedPtrExclusiveOwnership(benchmark::State& state) {
  while (state.KeepRunning()) {
    volatile auto var = std::make_shared<Foo>(0);
  }
}
BENCHMARK(BM_SharedPtrExclusiveOwnership);

} // namespace

BENCHMARK_MAIN();