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 }
timing.h
Timing utilities.
NonVirtualDerived::method
void method()
Definition: timeVirtual2.cpp:58
NonVirtualBase::NonVirtualBase
NonVirtualBase()
Definition: timeVirtual2.cpp:51
d
static const double d[K][N]
Definition: igam.h:11
b
Scalar * b
Definition: benchVecAdd.cpp:17
NonVirtualBase
Definition: timeVirtual2.cpp:50
VirtualBase::~VirtualBase
virtual ~VirtualBase()
Definition: timeVirtual2.cpp:40
DtorTestBase::~DtorTestBase
virtual ~DtorTestBase()
Definition: timeVirtual2.cpp:28
VirtualDerived::~VirtualDerived
~VirtualDerived() override
Definition: timeVirtual2.cpp:47
VirtualBase::VirtualBase
VirtualBase()
Definition: timeVirtual2.cpp:38
n
int n
Definition: BiCGSTAB_simple.cpp:1
VirtualDerived
Definition: timeVirtual2.cpp:43
data
int data[]
Definition: Map_placement_new.cpp:1
gttoc_
#define gttoc_(label)
Definition: timing.h:250
Virtual
Definition: timeVirtual.cpp:34
gttic_
#define gttic_(label)
Definition: timing.h:245
gtsam::tictoc_finishedIteration_
void tictoc_finishedIteration_()
Definition: timing.h:264
gtsam::tictoc_print_
void tictoc_print_()
Definition: timing.h:268
NonVirtualDerived::data
double data
Definition: timeVirtual2.cpp:56
VirtualBase
Definition: timeVirtual2.cpp:37
main
int main(int argc, char *argv[])
Definition: timeVirtual2.cpp:63
DtorTestDerived
Definition: timeVirtual2.cpp:31
NonVirtualDerived::~NonVirtualDerived
~NonVirtualDerived()
Definition: timeVirtual2.cpp:59
gtsam
traits
Definition: SFMdata.h:40
DtorTestDerived::DtorTestDerived
DtorTestDerived()
Definition: timeVirtual2.cpp:32
VirtualDerived::VirtualDerived
VirtualDerived()
Definition: timeVirtual2.cpp:45
std
Definition: BFloat16.h:88
DtorTestBase
Definition: timeVirtual2.cpp:26
NonVirtualDerived::NonVirtualDerived
NonVirtualDerived()
Definition: timeVirtual2.cpp:57
DtorTestBase::DtorTestBase
DtorTestBase()
Definition: timeVirtual2.cpp:27
VirtualDerived::data
double data
Definition: timeVirtual2.cpp:44
DtorTestDerived::~DtorTestDerived
~DtorTestDerived() override
Definition: timeVirtual2.cpp:33
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
NonVirtualDerived
Definition: timeVirtual2.cpp:55
VirtualDerived::method
void method() override
Definition: timeVirtual2.cpp:46
NonVirtualBase::~NonVirtualBase
~NonVirtualBase()
Definition: timeVirtual2.cpp:52


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:42:30