pointainer_speed.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2003-2005. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org/libs/ptr_container/
  10. //
  11. #include "test_data.hpp"
  12. #include <boost/ptr_container/ptr_vector.hpp>
  13. #include <boost/shared_ptr.hpp>
  14. #include <boost/progress.hpp>
  15. using namespace boost;
  16. using namespace std;
  17. typedef shared_ptr<Base> PolyPtr;
  18. struct PolyPtrOps
  19. {
  20. void operator()( const PolyPtr& a )
  21. { a->foo(); }
  22. };
  23. struct less_than
  24. {
  25. bool operator()( const PolyPtr& l, const PolyPtr& r ) const
  26. {
  27. return *l < *r;
  28. }
  29. bool operator()( const Base* l, const Base* r ) const
  30. {
  31. return *l < *r;
  32. }
  33. };
  34. struct greater_than
  35. {
  36. bool operator()( const PolyPtr& l, const PolyPtr& r ) const
  37. {
  38. return *l > *r;
  39. }
  40. bool operator()( const Base* l, const Base* r ) const
  41. {
  42. return *l > *r;
  43. }
  44. };
  45. struct data_less_than
  46. {
  47. bool operator()( const PolyPtr& l, const PolyPtr& r ) const
  48. {
  49. return l->data_less_than(*r);
  50. }
  51. bool operator()( const Base* l, const Base* r ) const
  52. {
  53. return l->data_less_than(*r);
  54. }
  55. };
  56. struct data_less_than2
  57. {
  58. bool operator()( const PolyPtr& l, const PolyPtr& r ) const
  59. {
  60. return l->data_less_than2(*r);
  61. }
  62. bool operator()( const Base* l, const Base* r ) const
  63. {
  64. return l->data_less_than2(*r);
  65. }
  66. };
  67. void test_speed()
  68. {
  69. enum { size = 50000 };
  70. vector<PolyPtr> svec;
  71. ptr_vector<Base> pvec;
  72. {
  73. progress_timer timer;
  74. for( int i = 0; i < size; ++i )
  75. svec.push_back( PolyPtr( new Derived ) );
  76. cout << "\n shared_ptr call new: ";
  77. }
  78. {
  79. progress_timer timer;
  80. for( int i = 0; i < size; ++i )
  81. pvec.push_back( new Derived );
  82. cout << "\n smart container call new: ";
  83. }
  84. {
  85. progress_timer timer;
  86. for_each( svec.begin(), svec.end(), PolyPtrOps() );
  87. cout << "\n shared_ptr call foo(): ";
  88. }
  89. {
  90. progress_timer timer;
  91. for_each( pvec.begin(), pvec.end(), mem_fun_ref( &Base::foo ) );
  92. cout << "\n smart container call foo(): ";
  93. }
  94. {
  95. progress_timer timer;
  96. sort( svec.begin(), svec.end(), less_than() );
  97. cout << "\n shared_ptr call sort(): ";
  98. }
  99. {
  100. progress_timer timer;
  101. sort( pvec.ptr_begin(), pvec.ptr_end(), less_than() );
  102. cout << "\n smart container call sort(): ";
  103. }
  104. {
  105. progress_timer timer;
  106. sort( svec.begin(), svec.end(), greater_than() );
  107. cout << "\n shared_ptr call sort() #2: ";
  108. }
  109. {
  110. progress_timer timer;
  111. sort( pvec.ptr_begin(), pvec.ptr_end(), greater_than() );
  112. cout << "\n smart container call sort() #2: ";
  113. }
  114. {
  115. progress_timer timer;
  116. sort( svec.begin(), svec.end(), data_less_than() );
  117. cout << "\n shared_ptr call sort() #3: ";
  118. }
  119. {
  120. progress_timer timer;
  121. sort( pvec.ptr_begin(), pvec.ptr_end(), data_less_than() );
  122. cout << "\n smart container call sort() #3: ";
  123. }
  124. {
  125. progress_timer timer;
  126. sort( svec.begin(), svec.end(), data_less_than2() );
  127. cout << "\n shared_ptr call sort() #4: ";
  128. }
  129. {
  130. progress_timer timer;
  131. sort( pvec.ptr_begin(), pvec.ptr_end(), data_less_than2() );
  132. cout << "\n smart container call sort() #4: ";
  133. }
  134. vector<Base*> copy1;
  135. for( ptr_vector<Base>::ptr_iterator i = pvec.ptr_begin(); i != pvec.ptr_end(); ++ i )
  136. copy1.push_back( *i );
  137. sort( pvec.ptr_begin(), pvec.ptr_end() );
  138. vector<Base*> copy2;
  139. for( ptr_vector<Base>::ptr_iterator i = pvec.ptr_begin(); i != pvec.ptr_end(); ++ i )
  140. copy2.push_back( *i );
  141. for( unsigned int i = 0; i < copy1.size(); ++i )
  142. {
  143. bool found = false;
  144. for( int j = 0; j < copy1.size(); ++ j )
  145. if( copy1[i] == copy2[j] )
  146. found = true;
  147. if( !found )
  148. cout << copy1[i] << endl;
  149. }
  150. BOOST_REQUIRE( pvec.size() == size );
  151. cout << endl;
  152. }
  153. #include <boost/test/unit_test.hpp>
  154. using boost::unit_test::test_suite;
  155. test_suite* init_unit_test_suite( int argc, char* argv[] )
  156. {
  157. test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
  158. test->add( BOOST_TEST_CASE( &test_speed ) );
  159. return test;
  160. }