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
12#include "test_data.hpp"
13#include <boost/ptr_container/ptr_vector.hpp>
14#include <boost/shared_ptr.hpp>
15#include <boost/progress.hpp>
16
17
18using namespace boost;
19using namespace std;
20
21
22typedef shared_ptr<Base> PolyPtr;
23
24struct PolyPtrOps
25{
26 void operator()( const PolyPtr& a )
27 { a->foo(); }
28};
29
30struct less_than
31{
32 bool operator()( const PolyPtr& l, const PolyPtr& r ) const
33 {
34 return *l < *r;
35 }
36
37 bool operator()( const Base* l, const Base* r ) const
38 {
39 return *l < *r;
40 }
41};
42
43struct greater_than
44{
45 bool operator()( const PolyPtr& l, const PolyPtr& r ) const
46 {
47 return *l > *r;
48 }
49
50 bool operator()( const Base* l, const Base* r ) const
51 {
52 return *l > *r;
53 }
54};
55
56struct data_less_than
57{
58 bool operator()( const PolyPtr& l, const PolyPtr& r ) const
59 {
60 return l->data_less_than(*r);
61 }
62
63 bool operator()( const Base* l, const Base* r ) const
64 {
65 return l->data_less_than(*r);
66 }
67};
68
69struct data_less_than2
70{
71 bool operator()( const PolyPtr& l, const PolyPtr& r ) const
72 {
73 return l->data_less_than2(*r);
74 }
75
76 bool operator()( const Base* l, const Base* r ) const
77 {
78 return l->data_less_than2(*r);
79 }
80};
81
82
83void test_speed()
84{
85 enum { size = 50000 };
86 vector<PolyPtr> svec;
87 ptr_vector<Base> pvec;
88
89 {
90 progress_timer timer;
91 for( int i = 0; i < size; ++i )
92 svec.push_back( PolyPtr( new Derived ) );
93 cout << "\n shared_ptr call new: ";
94 }
95
96 {
97 progress_timer timer;
98 for( int i = 0; i < size; ++i )
99 pvec.push_back( new Derived );
100 cout << "\n smart container call new: ";
101 }
102
103 {
104 progress_timer timer;
105 for_each( svec.begin(), svec.end(), PolyPtrOps() );
106 cout << "\n shared_ptr call foo(): ";
107 }
108
109 {
110 progress_timer timer;
111 for_each( pvec.begin(), pvec.end(), mem_fun_ref( &Base::foo ) );
112 cout << "\n smart container call foo(): ";
113 }
114
115 {
116 progress_timer timer;
117 sort( svec.begin(), svec.end(), less_than() );
118 cout << "\n shared_ptr call sort(): ";
119 }
120
121 {
122 progress_timer timer;
123 sort( pvec.ptr_begin(), pvec.ptr_end(), less_than() );
124 cout << "\n smart container call sort(): ";
125 }
126
127 {
128 progress_timer timer;
129 sort( svec.begin(), svec.end(), greater_than() );
130 cout << "\n shared_ptr call sort() #2: ";
131 }
132
133 {
134 progress_timer timer;
135 sort( pvec.ptr_begin(), pvec.ptr_end(), greater_than() );
136 cout << "\n smart container call sort() #2: ";
137 }
138
139 {
140 progress_timer timer;
141 sort( svec.begin(), svec.end(), data_less_than() );
142 cout << "\n shared_ptr call sort() #3: ";
143 }
144
145 {
146 progress_timer timer;
147 sort( pvec.ptr_begin(), pvec.ptr_end(), data_less_than() );
148 cout << "\n smart container call sort() #3: ";
149 }
150
151 {
152 progress_timer timer;
153 sort( svec.begin(), svec.end(), data_less_than2() );
154 cout << "\n shared_ptr call sort() #4: ";
155 }
156
157 {
158 progress_timer timer;
159 sort( pvec.ptr_begin(), pvec.ptr_end(), data_less_than2() );
160 cout << "\n smart container call sort() #4: ";
161 }
162
163 vector<Base*> copy1;
164 for( ptr_vector<Base>::ptr_iterator i = pvec.ptr_begin(); i != pvec.ptr_end(); ++ i )
165 copy1.push_back( *i );
166
167 sort( pvec.ptr_begin(), pvec.ptr_end() );
168
169
170 vector<Base*> copy2;
171 for( ptr_vector<Base>::ptr_iterator i = pvec.ptr_begin(); i != pvec.ptr_end(); ++ i )
172 copy2.push_back( *i );
173
174
175 for( unsigned int i = 0; i < copy1.size(); ++i )
176 {
177 bool found = false;
178 for( int j = 0; j < copy1.size(); ++ j )
179 if( copy1[i] == copy2[j] )
180 found = true;
181
182 if( !found )
183 cout << copy1[i] << endl;
184 }
185
186 BOOST_REQUIRE( pvec.size() == size );
187 cout << endl;
188}
189
190
191#include <boost/test/unit_test.hpp>
192using boost::unit_test::test_suite;
193
194test_suite* init_unit_test_suite( int argc, char* argv[] )
195{
196 test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
197
198 test->add( BOOST_TEST_CASE( &test_speed ) );
199
200 return test;
201}
202
203
204
205
206

source code of boost/libs/ptr_container/test/pointainer_speed.cpp