| 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 | |
| 12 | /** |
| 13 | * @file timeVirtual.cpp |
| 14 | * @brief Time the overhead of using virtual destructors and methods |
| 15 | * @author Richard Roberts |
| 16 | * @date Dec 3, 2010 |
| 17 | */ |
| 18 | |
| 19 | #include <gtsam/base/timing.h> |
| 20 | |
| 21 | #include <memory> |
| 22 | |
| 23 | #include <iostream> |
| 24 | |
| 25 | using namespace std; |
| 26 | using namespace gtsam; |
| 27 | |
| 28 | struct Plain { |
| 29 | size_t data; |
| 30 | Plain(size_t _data) : data(_data) {} |
| 31 | void setData(size_t data) { this->data = data; } |
| 32 | }; |
| 33 | |
| 34 | struct Virtual { |
| 35 | size_t data; |
| 36 | Virtual(size_t _data) : data(_data) {} |
| 37 | virtual void setData(size_t data) { this->data = data; } |
| 38 | virtual ~Virtual() {} |
| 39 | }; |
| 40 | |
| 41 | struct VirtualCounted { |
| 42 | size_t data; |
| 43 | size_t refCount; |
| 44 | VirtualCounted(size_t _data) : data(_data) {} |
| 45 | virtual void setData(size_t data) { this->data = data; } |
| 46 | virtual ~VirtualCounted() {} |
| 47 | }; |
| 48 | |
| 49 | int main(int argc, char *argv[]) { |
| 50 | |
| 51 | size_t trials = 10000000; |
| 52 | |
| 53 | gttic_(heap_plain_alloc_dealloc); |
| 54 | for(size_t i=0; i<trials; ++i) { |
| 55 | Plain *obj = new Plain(i); |
| 56 | delete obj; |
| 57 | } |
| 58 | gttoc_(heap_plain_alloc_dealloc); |
| 59 | |
| 60 | gttic_(heap_virtual_alloc_dealloc); |
| 61 | for(size_t i=0; i<trials; ++i) { |
| 62 | Virtual *obj = new Virtual(i); |
| 63 | delete obj; |
| 64 | } |
| 65 | gttoc_(heap_virtual_alloc_dealloc); |
| 66 | |
| 67 | gttic_(stack_plain_alloc_dealloc); |
| 68 | for(size_t i=0; i<trials; ++i) { |
| 69 | Plain obj(i); |
| 70 | } |
| 71 | gttoc_(stack_plain_alloc_dealloc); |
| 72 | |
| 73 | gttic_(stack_virtual_alloc_dealloc); |
| 74 | for(size_t i=0; i<trials; ++i) { |
| 75 | Virtual obj(i); |
| 76 | } |
| 77 | gttoc_(stack_virtual_alloc_dealloc); |
| 78 | |
| 79 | gttic_(shared_plain_alloc_dealloc); |
| 80 | for(size_t i=0; i<trials; ++i) { |
| 81 | std::shared_ptr<Plain> obj(new Plain(i)); |
| 82 | } |
| 83 | gttoc_(shared_plain_alloc_dealloc); |
| 84 | |
| 85 | gttic_(shared_virtual_alloc_dealloc); |
| 86 | for(size_t i=0; i<trials; ++i) { |
| 87 | std::shared_ptr<Virtual> obj(new Virtual(i)); |
| 88 | } |
| 89 | gttoc_(shared_virtual_alloc_dealloc); |
| 90 | |
| 91 | |
| 92 | gttic_(heap_plain_alloc_dealloc_call); |
| 93 | for(size_t i=0; i<trials; ++i) { |
| 94 | Plain *obj = new Plain(i); |
| 95 | obj->setData(i+1); |
| 96 | delete obj; |
| 97 | } |
| 98 | gttoc_(heap_plain_alloc_dealloc_call); |
| 99 | |
| 100 | gttic_(heap_virtual_alloc_dealloc_call); |
| 101 | for(size_t i=0; i<trials; ++i) { |
| 102 | Virtual *obj = new Virtual(i); |
| 103 | obj->setData(i+1); |
| 104 | delete obj; |
| 105 | } |
| 106 | gttoc_(heap_virtual_alloc_dealloc_call); |
| 107 | |
| 108 | gttic_(stack_plain_alloc_dealloc_call); |
| 109 | for(size_t i=0; i<trials; ++i) { |
| 110 | Plain obj(i); |
| 111 | obj.setData(i+1); |
| 112 | } |
| 113 | gttoc_(stack_plain_alloc_dealloc_call); |
| 114 | |
| 115 | gttic_(stack_virtual_alloc_dealloc_call); |
| 116 | for(size_t i=0; i<trials; ++i) { |
| 117 | Virtual obj(i); |
| 118 | obj.setData(i+1); |
| 119 | } |
| 120 | gttoc_(stack_virtual_alloc_dealloc_call); |
| 121 | |
| 122 | gttic_(shared_plain_alloc_dealloc_call); |
| 123 | for(size_t i=0; i<trials; ++i) { |
| 124 | std::shared_ptr<Plain> obj(new Plain(i)); |
| 125 | obj->setData(i+1); |
| 126 | } |
| 127 | gttoc_(shared_plain_alloc_dealloc_call); |
| 128 | |
| 129 | gttic_(shared_virtual_alloc_dealloc_call); |
| 130 | for(size_t i=0; i<trials; ++i) { |
| 131 | std::shared_ptr<Virtual> obj(new Virtual(i)); |
| 132 | obj->setData(i+1); |
| 133 | } |
| 134 | gttoc_(shared_virtual_alloc_dealloc_call); |
| 135 | |
| 136 | gttic_(intrusive_virtual_alloc_dealloc_call); |
| 137 | for(size_t i=0; i<trials; ++i) { |
| 138 | std::shared_ptr<VirtualCounted> obj(new VirtualCounted(i)); |
| 139 | obj->setData(i+1); |
| 140 | } |
| 141 | gttoc_(intrusive_virtual_alloc_dealloc_call); |
| 142 | |
| 143 | tictoc_print_(); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |