| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							|  |  |  |  * Atlanta, Georgia 30332-0415 | 
					
						
							|  |  |  |  * All Rights Reserved | 
					
						
							|  |  |  |  * Authors: Frank Dellaert, et al. (see THANKS for the full author list) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * See LICENSE for the license information | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * @file    timeVirtual.cpp | 
					
						
							|  |  |  |  * @brief   Time the overhead of using virtual destructors and methods | 
					
						
							|  |  |  |  * @author  Richard Roberts | 
					
						
							|  |  |  |  * @date    Nov 6, 2011 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/base/timing.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace boost; | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct DtorTestBase { | 
					
						
							|  |  |  |   DtorTestBase() { cout << "  DtorTestBase" << endl; } | 
					
						
							|  |  |  |   virtual ~DtorTestBase() { cout << "  ~DtorTestBase" << endl; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct DtorTestDerived : public DtorTestBase { | 
					
						
							|  |  |  |   DtorTestDerived() { cout << "  DtorTestDerived" << endl; } | 
					
						
							| 
									
										
										
										
											2021-01-29 12:02:13 +08:00
										 |  |  |   ~DtorTestDerived() override { cout << "  ~DtorTestDerived" << endl; } | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct VirtualBase { | 
					
						
							|  |  |  |   VirtualBase() { } | 
					
						
							|  |  |  |   virtual void method() = 0; | 
					
						
							|  |  |  |   virtual ~VirtualBase() { } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct VirtualDerived : public VirtualBase { | 
					
						
							|  |  |  |   double data; | 
					
						
							|  |  |  |   VirtualDerived() { data = rand(); } | 
					
						
							| 
									
										
										
										
											2021-01-29 12:02:13 +08:00
										 |  |  |   void method() override { data = rand(); } | 
					
						
							|  |  |  |   ~VirtualDerived() override { } | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct NonVirtualBase { | 
					
						
							|  |  |  |   NonVirtualBase() { } | 
					
						
							|  |  |  |   ~NonVirtualBase() { } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct NonVirtualDerived : public NonVirtualBase { | 
					
						
							|  |  |  |   double data; | 
					
						
							|  |  |  |   NonVirtualDerived() { data = rand(); } | 
					
						
							|  |  |  |   void method() { data = rand(); } | 
					
						
							|  |  |  |   ~NonVirtualDerived() { } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char *argv[]) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Virtual destructor test
 | 
					
						
							|  |  |  |   cout << "Stack objects:" << endl; | 
					
						
							|  |  |  |   cout << "Base:" << endl; | 
					
						
							|  |  |  |   { DtorTestBase b; } | 
					
						
							|  |  |  |   cout << "Derived:" << endl; | 
					
						
							|  |  |  |   { DtorTestDerived d; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   cout << "Heap objects:" << endl; | 
					
						
							|  |  |  |   cout << "Base:" << endl; | 
					
						
							|  |  |  |   { DtorTestBase *b = new DtorTestBase(); delete b; } | 
					
						
							|  |  |  |   cout << "Derived:" << endl; | 
					
						
							|  |  |  |   { DtorTestDerived *d = new DtorTestDerived(); delete d; } | 
					
						
							|  |  |  |   cout << "Derived with base pointer:" << endl; | 
					
						
							|  |  |  |   { DtorTestBase *b = new DtorTestDerived(); delete b; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   int n = 10000000; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-03 02:49:48 +08:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   VirtualBase** b = new VirtualBase*[n]; | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttic_(Virtual); | 
					
						
							|  |  |  |   gttic_(new); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   for(int i=0; i<n; ++i) | 
					
						
							|  |  |  |     b[i] = new VirtualDerived(); | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttoc_(new); | 
					
						
							|  |  |  |   gttic_(method); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   for(int i=0; i<n; ++i) | 
					
						
							|  |  |  |     b[i]->method(); | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttoc_(method); | 
					
						
							|  |  |  |   gttic_(dynamic_cast); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   for(int i=0; i<n; ++i) { | 
					
						
							|  |  |  |     VirtualDerived* d = dynamic_cast<VirtualDerived*>(b[i]); | 
					
						
							|  |  |  |     if(d) | 
					
						
							|  |  |  |       d->method(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttoc_(dynamic_cast); | 
					
						
							|  |  |  |   gttic_(delete); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   for(int i=0; i<n; ++i) | 
					
						
							|  |  |  |     delete b[i]; | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttoc_(delete); | 
					
						
							|  |  |  |   gttoc_(Virtual); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   delete[] b; | 
					
						
							| 
									
										
										
										
											2012-10-03 02:49:48 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-03 02:49:48 +08:00
										 |  |  |   { | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   NonVirtualDerived** d = new NonVirtualDerived*[n]; | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttic_(NonVirtual); | 
					
						
							|  |  |  |   gttic_(new); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   for(int i=0; i<n; ++i) | 
					
						
							|  |  |  |     d[i] = new NonVirtualDerived(); | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttoc_(new); | 
					
						
							|  |  |  |   gttic_(method); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   for(int i=0; i<n; ++i) | 
					
						
							|  |  |  |     d[i]->method(); | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttoc_(method); | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(dynamic_cast_does_nothing); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   for(int i=0; i<n; ++i) | 
					
						
							|  |  |  |     d[i]->method(); | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(dynamic_cast_does_nothing); | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttic_(delete); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   for(int i=0; i<n; ++i) | 
					
						
							|  |  |  |     delete d[i]; | 
					
						
							| 
									
										
										
										
											2012-10-03 04:18:41 +08:00
										 |  |  |   gttoc_(delete); | 
					
						
							|  |  |  |   gttoc_(NonVirtual); | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  |   delete[] d; | 
					
						
							| 
									
										
										
										
											2012-10-03 02:49:48 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2011-11-07 02:19:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   tictoc_finishedIteration_(); | 
					
						
							|  |  |  |   tictoc_print_(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } |