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


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:20