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