shared_ptr_alloc_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // shared_ptr_alloc_test.cpp - use to evaluate the impact of count allocations
  3. //
  4. // Copyright (c) 2002, 2003 Peter Dimov
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <boost/shared_ptr.hpp>
  11. #include <boost/config.hpp>
  12. #include <boost/detail/quick_allocator.hpp>
  13. #include <iostream>
  14. #include <vector>
  15. #include <ctime>
  16. #include <cstddef>
  17. #include <memory>
  18. int const n = 1024 * 1024;
  19. template<class T> void test(T * = 0)
  20. {
  21. std::clock_t t = std::clock();
  22. std::clock_t t2;
  23. {
  24. std::vector< boost::shared_ptr<T> > v;
  25. for(int i = 0; i < n; ++i)
  26. {
  27. boost::shared_ptr<T> pi(new T(i));
  28. v.push_back(pi);
  29. }
  30. t2 = std::clock();
  31. }
  32. std::clock_t t3 = std::clock();
  33. std::cout << " " << static_cast<double>(t3 - t) / CLOCKS_PER_SEC << " seconds, " << static_cast<double>(t2 - t) / CLOCKS_PER_SEC << " + " << static_cast<double>(t3 - t2) / CLOCKS_PER_SEC << ".\n";
  34. }
  35. class X
  36. {
  37. public:
  38. explicit X(int n): n_(n)
  39. {
  40. }
  41. void * operator new(std::size_t)
  42. {
  43. return std::allocator<X>().allocate(1, static_cast<X*>(0));
  44. }
  45. void operator delete(void * p)
  46. {
  47. std::allocator<X>().deallocate(static_cast<X*>(p), 1);
  48. }
  49. private:
  50. X(X const &);
  51. X & operator=(X const &);
  52. int n_;
  53. };
  54. class Y
  55. {
  56. public:
  57. explicit Y(int n): n_(n)
  58. {
  59. }
  60. void * operator new(std::size_t n)
  61. {
  62. return boost::detail::quick_allocator<Y>::alloc(n);
  63. }
  64. void operator delete(void * p, std::size_t n)
  65. {
  66. boost::detail::quick_allocator<Y>::dealloc(p, n);
  67. }
  68. private:
  69. Y(Y const &);
  70. Y & operator=(Y const &);
  71. int n_;
  72. };
  73. class Z: public Y
  74. {
  75. public:
  76. explicit Z(int n): Y(n), m_(n + 1)
  77. {
  78. }
  79. private:
  80. Z(Z const &);
  81. Z & operator=(Z const &);
  82. int m_;
  83. };
  84. int main()
  85. {
  86. std::cout << BOOST_COMPILER "\n";
  87. std::cout << BOOST_PLATFORM "\n";
  88. std::cout << BOOST_STDLIB "\n";
  89. #if defined(BOOST_HAS_THREADS)
  90. std::cout << "BOOST_HAS_THREADS: (defined)\n";
  91. #else
  92. std::cout << "BOOST_HAS_THREADS: (not defined)\n";
  93. #endif
  94. #if defined(BOOST_SP_USE_STD_ALLOCATOR)
  95. std::cout << "BOOST_SP_USE_STD_ALLOCATOR: (defined)\n";
  96. #else
  97. std::cout << "BOOST_SP_USE_STD_ALLOCATOR: (not defined)\n";
  98. #endif
  99. #if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
  100. std::cout << "BOOST_SP_USE_QUICK_ALLOCATOR: (defined)\n";
  101. #else
  102. std::cout << "BOOST_SP_USE_QUICK_ALLOCATOR: (not defined)\n";
  103. #endif
  104. #if defined(BOOST_QA_PAGE_SIZE)
  105. std::cout << "BOOST_QA_PAGE_SIZE: " << BOOST_QA_PAGE_SIZE << "\n";
  106. #else
  107. std::cout << "BOOST_QA_PAGE_SIZE: (not defined)\n";
  108. #endif
  109. std::cout << n << " shared_ptr<int> allocations + deallocations:\n";
  110. test<int>();
  111. test<int>();
  112. test<int>();
  113. std::cout << n << " shared_ptr<X> allocations + deallocations:\n";
  114. test<X>();
  115. test<X>();
  116. test<X>();
  117. std::cout << n << " shared_ptr<Y> allocations + deallocations:\n";
  118. test<Y>();
  119. test<Y>();
  120. test<Y>();
  121. std::cout << n << " shared_ptr<Z> allocations + deallocations:\n";
  122. test<Z>();
  123. test<Z>();
  124. test<Z>();
  125. }