timeVirtual2.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 DtorTestBase {
31  DtorTestBase() { cout << " DtorTestBase" << endl; }
32  virtual ~DtorTestBase() { cout << " ~DtorTestBase" << endl; }
33 };
34 
35 struct DtorTestDerived : public DtorTestBase {
36  DtorTestDerived() { cout << " DtorTestDerived" << endl; }
37  ~DtorTestDerived() override { cout << " ~DtorTestDerived" << endl; }
38 };
39 
40 
41 struct VirtualBase {
43  virtual void method() = 0;
44  virtual ~VirtualBase() { }
45 };
46 
47 struct VirtualDerived : public VirtualBase {
48  double data;
49  VirtualDerived() { data = rand(); }
50  void method() override { data = rand(); }
51  ~VirtualDerived() override { }
52 };
53 
57 };
58 
60  double data;
61  NonVirtualDerived() { data = rand(); }
62  void method() { data = rand(); }
64 };
65 
66 
67 int main(int argc, char *argv[]) {
68 
69  // Virtual destructor test
70  cout << "Stack objects:" << endl;
71  cout << "Base:" << endl;
72  { DtorTestBase b; }
73  cout << "Derived:" << endl;
74  { DtorTestDerived d; }
75 
76  cout << "Heap objects:" << endl;
77  cout << "Base:" << endl;
78  { DtorTestBase *b = new DtorTestBase(); delete b; }
79  cout << "Derived:" << endl;
80  { DtorTestDerived *d = new DtorTestDerived(); delete d; }
81  cout << "Derived with base pointer:" << endl;
82  { DtorTestBase *b = new DtorTestDerived(); delete b; }
83 
84  int n = 10000000;
85 
86  {
87  VirtualBase** b = new VirtualBase*[n];
88  gttic_(Virtual);
89  gttic_(new);
90  for(int i=0; i<n; ++i)
91  b[i] = new VirtualDerived();
92  gttoc_(new);
93  gttic_(method);
94  for(int i=0; i<n; ++i)
95  b[i]->method();
96  gttoc_(method);
97  gttic_(dynamic_cast);
98  for(int i=0; i<n; ++i) {
99  VirtualDerived* d = dynamic_cast<VirtualDerived*>(b[i]);
100  if(d)
101  d->method();
102  }
103  gttoc_(dynamic_cast);
104  gttic_(delete);
105  for(int i=0; i<n; ++i)
106  delete b[i];
107  gttoc_(delete);
108  gttoc_(Virtual);
109  delete[] b;
110  }
111 
112 
113  {
115  gttic_(NonVirtual);
116  gttic_(new);
117  for(int i=0; i<n; ++i)
118  d[i] = new NonVirtualDerived();
119  gttoc_(new);
120  gttic_(method);
121  for(int i=0; i<n; ++i)
122  d[i]->method();
123  gttoc_(method);
124  gttic_(dynamic_cast_does_nothing);
125  for(int i=0; i<n; ++i)
126  d[i]->method();
127  gttoc_(dynamic_cast_does_nothing);
128  gttic_(delete);
129  for(int i=0; i<n; ++i)
130  delete d[i];
131  gttoc_(delete);
132  gttoc_(NonVirtual);
133  delete[] d;
134  }
135 
137  tictoc_print_();
138 
139  return 0;
140 }
~VirtualDerived() override
Scalar * b
Definition: benchVecAdd.cpp:17
#define gttic_(label)
Definition: timing.h:230
int n
Definition: Half.h:150
void method() override
void tictoc_print_()
Definition: timing.h:253
virtual ~DtorTestBase()
traits
Definition: chartTesting.h:28
virtual ~VirtualBase()
void tictoc_finishedIteration_()
Definition: timing.h:249
#define gttoc_(label)
Definition: timing.h:235
int main(int argc, char *argv[])
Timing utilities.
~DtorTestDerived() override


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