| 
									
										
										
										
											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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    timeVirtual.cpp | 
					
						
							|  |  |  |  * @brief   Time the overhead of using virtual destructors and methods | 
					
						
							|  |  |  |  * @author  Richard Roberts | 
					
						
							| 
									
										
										
										
											2011-10-23 03:48:52 +08:00
										 |  |  |  * @date    Dec 3, 2010 | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/base/timing.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  | #include <memory>
 | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct Plain { | 
					
						
							|  |  |  |   size_t data; | 
					
						
							|  |  |  |   Plain(size_t _data) : data(_data) {} | 
					
						
							|  |  |  |   void setData(size_t data) { this->data = data; } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct Virtual { | 
					
						
							|  |  |  |   size_t data; | 
					
						
							|  |  |  |   Virtual(size_t _data) : data(_data) {} | 
					
						
							|  |  |  |   virtual void setData(size_t data) { this->data = data; } | 
					
						
							|  |  |  |   virtual ~Virtual() {} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct VirtualCounted { | 
					
						
							|  |  |  |   size_t data; | 
					
						
							|  |  |  |   size_t refCount; | 
					
						
							|  |  |  |   VirtualCounted(size_t _data) : data(_data) {} | 
					
						
							|  |  |  |   virtual void setData(size_t data) { this->data = data; } | 
					
						
							|  |  |  |   virtual ~VirtualCounted() {} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char *argv[]) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   size_t trials = 10000000; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(heap_plain_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							|  |  |  |     Plain *obj = new Plain(i); | 
					
						
							|  |  |  |     delete obj; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(heap_plain_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(heap_virtual_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							|  |  |  |     Virtual *obj = new Virtual(i); | 
					
						
							|  |  |  |     delete obj; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(heap_virtual_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(stack_plain_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							|  |  |  |     Plain obj(i); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(stack_plain_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(stack_virtual_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							|  |  |  |     Virtual obj(i); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(stack_virtual_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(shared_plain_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |     std::shared_ptr<Plain> obj(new Plain(i)); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(shared_plain_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(shared_virtual_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |     std::shared_ptr<Virtual> obj(new Virtual(i)); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(shared_virtual_alloc_dealloc); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(heap_plain_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							|  |  |  |     Plain *obj = new Plain(i); | 
					
						
							|  |  |  |     obj->setData(i+1); | 
					
						
							|  |  |  |     delete obj; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(heap_plain_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(heap_virtual_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							|  |  |  |     Virtual *obj = new Virtual(i); | 
					
						
							|  |  |  |     obj->setData(i+1); | 
					
						
							|  |  |  |     delete obj; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(heap_virtual_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(stack_plain_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							|  |  |  |     Plain obj(i); | 
					
						
							|  |  |  |     obj.setData(i+1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(stack_plain_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(stack_virtual_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							|  |  |  |     Virtual obj(i); | 
					
						
							|  |  |  |     obj.setData(i+1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(stack_virtual_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(shared_plain_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |     std::shared_ptr<Plain> obj(new Plain(i)); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |     obj->setData(i+1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(shared_plain_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(shared_virtual_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							| 
									
										
										
										
											2023-01-18 06:05:12 +08:00
										 |  |  |     std::shared_ptr<Virtual> obj(new Virtual(i)); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |     obj->setData(i+1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(shared_virtual_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttic_(intrusive_virtual_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |   for(size_t i=0; i<trials; ++i) { | 
					
						
							| 
									
										
										
										
											2023-02-06 12:28:12 +08:00
										 |  |  |     std::shared_ptr<VirtualCounted> obj(new VirtualCounted(i)); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  |     obj->setData(i+1); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-02-06 05:52:49 +08:00
										 |  |  |   gttoc_(intrusive_virtual_alloc_dealloc_call); | 
					
						
							| 
									
										
										
										
											2010-12-09 09:32:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   tictoc_print_(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } |