timeVirtual2.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Nov 6, 2011
  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 DtorTestBase {
  22. DtorTestBase() { cout << " DtorTestBase" << endl; }
  23. virtual ~DtorTestBase() { cout << " ~DtorTestBase" << endl; }
  24. };
  25. struct DtorTestDerived : public DtorTestBase {
  26. DtorTestDerived() { cout << " DtorTestDerived" << endl; }
  27. ~DtorTestDerived() override { cout << " ~DtorTestDerived" << endl; }
  28. };
  29. struct VirtualBase {
  30. VirtualBase() { }
  31. virtual void method() = 0;
  32. virtual ~VirtualBase() { }
  33. };
  34. struct VirtualDerived : public VirtualBase {
  35. double data;
  36. VirtualDerived() { data = rand(); }
  37. void method() override { data = rand(); }
  38. ~VirtualDerived() override { }
  39. };
  40. struct NonVirtualBase {
  41. NonVirtualBase() { }
  42. ~NonVirtualBase() { }
  43. };
  44. struct NonVirtualDerived : public NonVirtualBase {
  45. double data;
  46. NonVirtualDerived() { data = rand(); }
  47. void method() { data = rand(); }
  48. ~NonVirtualDerived() { }
  49. };
  50. int main(int argc, char *argv[]) {
  51. // Virtual destructor test
  52. cout << "Stack objects:" << endl;
  53. cout << "Base:" << endl;
  54. { DtorTestBase b; }
  55. cout << "Derived:" << endl;
  56. { DtorTestDerived d; }
  57. cout << "Heap objects:" << endl;
  58. cout << "Base:" << endl;
  59. { DtorTestBase *b = new DtorTestBase(); delete b; }
  60. cout << "Derived:" << endl;
  61. { DtorTestDerived *d = new DtorTestDerived(); delete d; }
  62. cout << "Derived with base pointer:" << endl;
  63. { DtorTestBase *b = new DtorTestDerived(); delete b; }
  64. int n = 10000000;
  65. {
  66. VirtualBase** b = new VirtualBase*[n];
  67. gttic_(Virtual);
  68. gttic_(new);
  69. for(int i=0; i<n; ++i)
  70. b[i] = new VirtualDerived();
  71. gttoc_(new);
  72. gttic_(method);
  73. for(int i=0; i<n; ++i)
  74. b[i]->method();
  75. gttoc_(method);
  76. gttic_(dynamic_cast);
  77. for(int i=0; i<n; ++i) {
  78. VirtualDerived* d = dynamic_cast<VirtualDerived*>(b[i]);
  79. if(d)
  80. d->method();
  81. }
  82. gttoc_(dynamic_cast);
  83. gttic_(delete);
  84. for(int i=0; i<n; ++i)
  85. delete b[i];
  86. gttoc_(delete);
  87. gttoc_(Virtual);
  88. delete[] b;
  89. }
  90. {
  91. NonVirtualDerived** d = new NonVirtualDerived*[n];
  92. gttic_(NonVirtual);
  93. gttic_(new);
  94. for(int i=0; i<n; ++i)
  95. d[i] = new NonVirtualDerived();
  96. gttoc_(new);
  97. gttic_(method);
  98. for(int i=0; i<n; ++i)
  99. d[i]->method();
  100. gttoc_(method);
  101. gttic_(dynamic_cast_does_nothing);
  102. for(int i=0; i<n; ++i)
  103. d[i]->method();
  104. gttoc_(dynamic_cast_does_nothing);
  105. gttic_(delete);
  106. for(int i=0; i<n; ++i)
  107. delete d[i];
  108. gttoc_(delete);
  109. gttoc_(NonVirtual);
  110. delete[] d;
  111. }
  112. tictoc_finishedIteration_();
  113. tictoc_print_();
  114. return 0;
  115. }