timeVirtual.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file timeVirtual.cpp
  10. * @brief Time the overhead of using virtual destructors and methods
  11. * @author Richard Roberts
  12. * @date Dec 3, 2010
  13. */
  14. #include <gtsam/base/timing.h>
  15. #include <boost/shared_ptr.hpp>
  16. #include <boost/intrusive_ptr.hpp>
  17. #include <iostream>
  18. using namespace std;
  19. using namespace boost;
  20. using namespace gtsam;
  21. struct Plain {
  22. size_t data;
  23. Plain(size_t _data) : data(_data) {}
  24. void setData(size_t data) { this->data = data; }
  25. };
  26. struct Virtual {
  27. size_t data;
  28. Virtual(size_t _data) : data(_data) {}
  29. virtual void setData(size_t data) { this->data = data; }
  30. virtual ~Virtual() {}
  31. };
  32. struct VirtualCounted {
  33. size_t data;
  34. size_t refCount;
  35. VirtualCounted(size_t _data) : data(_data) {}
  36. virtual void setData(size_t data) { this->data = data; }
  37. virtual ~VirtualCounted() {}
  38. };
  39. void intrusive_ptr_add_ref(VirtualCounted* obj) { ++ obj->refCount; }
  40. void intrusive_ptr_release(VirtualCounted* obj) {
  41. assert(obj->refCount > 0);
  42. -- obj->refCount;
  43. if(obj->refCount == 0)
  44. delete obj;
  45. }
  46. int main(int argc, char *argv[]) {
  47. size_t trials = 10000000;
  48. gttic_(heap_plain_alloc_dealloc);
  49. for(size_t i=0; i<trials; ++i) {
  50. Plain *obj = new Plain(i);
  51. delete obj;
  52. }
  53. gttoc_(heap_plain_alloc_dealloc);
  54. gttic_(heap_virtual_alloc_dealloc);
  55. for(size_t i=0; i<trials; ++i) {
  56. Virtual *obj = new Virtual(i);
  57. delete obj;
  58. }
  59. gttoc_(heap_virtual_alloc_dealloc);
  60. gttic_(stack_plain_alloc_dealloc);
  61. for(size_t i=0; i<trials; ++i) {
  62. Plain obj(i);
  63. }
  64. gttoc_(stack_plain_alloc_dealloc);
  65. gttic_(stack_virtual_alloc_dealloc);
  66. for(size_t i=0; i<trials; ++i) {
  67. Virtual obj(i);
  68. }
  69. gttoc_(stack_virtual_alloc_dealloc);
  70. gttic_(shared_plain_alloc_dealloc);
  71. for(size_t i=0; i<trials; ++i) {
  72. boost::shared_ptr<Plain> obj(new Plain(i));
  73. }
  74. gttoc_(shared_plain_alloc_dealloc);
  75. gttic_(shared_virtual_alloc_dealloc);
  76. for(size_t i=0; i<trials; ++i) {
  77. boost::shared_ptr<Virtual> obj(new Virtual(i));
  78. }
  79. gttoc_(shared_virtual_alloc_dealloc);
  80. gttic_(heap_plain_alloc_dealloc_call);
  81. for(size_t i=0; i<trials; ++i) {
  82. Plain *obj = new Plain(i);
  83. obj->setData(i+1);
  84. delete obj;
  85. }
  86. gttoc_(heap_plain_alloc_dealloc_call);
  87. gttic_(heap_virtual_alloc_dealloc_call);
  88. for(size_t i=0; i<trials; ++i) {
  89. Virtual *obj = new Virtual(i);
  90. obj->setData(i+1);
  91. delete obj;
  92. }
  93. gttoc_(heap_virtual_alloc_dealloc_call);
  94. gttic_(stack_plain_alloc_dealloc_call);
  95. for(size_t i=0; i<trials; ++i) {
  96. Plain obj(i);
  97. obj.setData(i+1);
  98. }
  99. gttoc_(stack_plain_alloc_dealloc_call);
  100. gttic_(stack_virtual_alloc_dealloc_call);
  101. for(size_t i=0; i<trials; ++i) {
  102. Virtual obj(i);
  103. obj.setData(i+1);
  104. }
  105. gttoc_(stack_virtual_alloc_dealloc_call);
  106. gttic_(shared_plain_alloc_dealloc_call);
  107. for(size_t i=0; i<trials; ++i) {
  108. boost::shared_ptr<Plain> obj(new Plain(i));
  109. obj->setData(i+1);
  110. }
  111. gttoc_(shared_plain_alloc_dealloc_call);
  112. gttic_(shared_virtual_alloc_dealloc_call);
  113. for(size_t i=0; i<trials; ++i) {
  114. boost::shared_ptr<Virtual> obj(new Virtual(i));
  115. obj->setData(i+1);
  116. }
  117. gttoc_(shared_virtual_alloc_dealloc_call);
  118. gttic_(intrusive_virtual_alloc_dealloc_call);
  119. for(size_t i=0; i<trials; ++i) {
  120. intrusive_ptr<VirtualCounted> obj(new VirtualCounted(i));
  121. obj->setData(i+1);
  122. }
  123. gttoc_(intrusive_virtual_alloc_dealloc_call);
  124. tictoc_print_();
  125. return 0;
  126. }