timeVirtual.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #include <gtsam/base/timing.h>
20 
21 #include <boost/shared_ptr.hpp>
22 #include <boost/intrusive_ptr.hpp>
23 
24 #include <iostream>
25 
26 using namespace std;
27 using namespace boost;
28 using namespace gtsam;
29 
30 struct Plain {
31  size_t data;
32  Plain(size_t _data) : data(_data) {}
33  void setData(size_t data) { this->data = data; }
34 };
35 
36 struct Virtual {
37  size_t data;
38  Virtual(size_t _data) : data(_data) {}
39  virtual void setData(size_t data) { this->data = data; }
40  virtual ~Virtual() {}
41 };
42 
44  size_t data;
45  size_t refCount;
46  VirtualCounted(size_t _data) : data(_data) {}
47  virtual void setData(size_t data) { this->data = data; }
48  virtual ~VirtualCounted() {}
49 };
50 
53  assert(obj->refCount > 0);
54  -- obj->refCount;
55  if(obj->refCount == 0)
56  delete obj;
57 }
58 
59 int main(int argc, char *argv[]) {
60 
61  size_t trials = 10000000;
62 
63  gttic_(heap_plain_alloc_dealloc);
64  for(size_t i=0; i<trials; ++i) {
65  Plain *obj = new Plain(i);
66  delete obj;
67  }
68  gttoc_(heap_plain_alloc_dealloc);
69 
70  gttic_(heap_virtual_alloc_dealloc);
71  for(size_t i=0; i<trials; ++i) {
72  Virtual *obj = new Virtual(i);
73  delete obj;
74  }
75  gttoc_(heap_virtual_alloc_dealloc);
76 
77  gttic_(stack_plain_alloc_dealloc);
78  for(size_t i=0; i<trials; ++i) {
79  Plain obj(i);
80  }
81  gttoc_(stack_plain_alloc_dealloc);
82 
83  gttic_(stack_virtual_alloc_dealloc);
84  for(size_t i=0; i<trials; ++i) {
85  Virtual obj(i);
86  }
87  gttoc_(stack_virtual_alloc_dealloc);
88 
89  gttic_(shared_plain_alloc_dealloc);
90  for(size_t i=0; i<trials; ++i) {
91  boost::shared_ptr<Plain> obj(new Plain(i));
92  }
93  gttoc_(shared_plain_alloc_dealloc);
94 
95  gttic_(shared_virtual_alloc_dealloc);
96  for(size_t i=0; i<trials; ++i) {
97  boost::shared_ptr<Virtual> obj(new Virtual(i));
98  }
99  gttoc_(shared_virtual_alloc_dealloc);
100 
101 
102  gttic_(heap_plain_alloc_dealloc_call);
103  for(size_t i=0; i<trials; ++i) {
104  Plain *obj = new Plain(i);
105  obj->setData(i+1);
106  delete obj;
107  }
108  gttoc_(heap_plain_alloc_dealloc_call);
109 
110  gttic_(heap_virtual_alloc_dealloc_call);
111  for(size_t i=0; i<trials; ++i) {
112  Virtual *obj = new Virtual(i);
113  obj->setData(i+1);
114  delete obj;
115  }
116  gttoc_(heap_virtual_alloc_dealloc_call);
117 
118  gttic_(stack_plain_alloc_dealloc_call);
119  for(size_t i=0; i<trials; ++i) {
120  Plain obj(i);
121  obj.setData(i+1);
122  }
123  gttoc_(stack_plain_alloc_dealloc_call);
124 
125  gttic_(stack_virtual_alloc_dealloc_call);
126  for(size_t i=0; i<trials; ++i) {
127  Virtual obj(i);
128  obj.setData(i+1);
129  }
130  gttoc_(stack_virtual_alloc_dealloc_call);
131 
132  gttic_(shared_plain_alloc_dealloc_call);
133  for(size_t i=0; i<trials; ++i) {
134  boost::shared_ptr<Plain> obj(new Plain(i));
135  obj->setData(i+1);
136  }
137  gttoc_(shared_plain_alloc_dealloc_call);
138 
139  gttic_(shared_virtual_alloc_dealloc_call);
140  for(size_t i=0; i<trials; ++i) {
141  boost::shared_ptr<Virtual> obj(new Virtual(i));
142  obj->setData(i+1);
143  }
144  gttoc_(shared_virtual_alloc_dealloc_call);
145 
146  gttic_(intrusive_virtual_alloc_dealloc_call);
147  for(size_t i=0; i<trials; ++i) {
148  intrusive_ptr<VirtualCounted> obj(new VirtualCounted(i));
149  obj->setData(i+1);
150  }
151  gttoc_(intrusive_virtual_alloc_dealloc_call);
152 
153  tictoc_print_();
154 
155  return 0;
156 }
virtual ~VirtualCounted()
Definition: timeVirtual.cpp:48
VirtualCounted(size_t _data)
Definition: timeVirtual.cpp:46
#define gttic_(label)
Definition: timing.h:230
Definition: Half.h:150
virtual void setData(size_t data)
Definition: timeVirtual.cpp:39
void setData(size_t data)
Definition: timeVirtual.cpp:33
size_t data
Definition: timeVirtual.cpp:31
Virtual(size_t _data)
Definition: timeVirtual.cpp:38
int data[]
void tictoc_print_()
Definition: timing.h:253
Plain(size_t _data)
Definition: timeVirtual.cpp:32
traits
Definition: chartTesting.h:28
virtual void setData(size_t data)
Definition: timeVirtual.cpp:47
int main(int argc, char *argv[])
Definition: timeVirtual.cpp:59
size_t data
Definition: timeVirtual.cpp:37
void intrusive_ptr_release(VirtualCounted *obj)
Definition: timeVirtual.cpp:52
virtual ~Virtual()
Definition: timeVirtual.cpp:40
void intrusive_ptr_add_ref(VirtualCounted *obj)
Definition: timeVirtual.cpp:51
#define gttoc_(label)
Definition: timing.h:235
Timing utilities.


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:04