1 /* Boost.MultiIndex performance test.
3 * Copyright 2003-2013 Joaquin M Lopez Munoz.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
8 * See http://www.boost.org/libs/multi_index for library home page.
11 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
15 #include <boost/multi_index_container.hpp>
16 #include <boost/multi_index/identity.hpp>
17 #include <boost/multi_index/ordered_index.hpp>
18 #include <boost/multi_index/sequenced_index.hpp>
19 #include <boost/next_prior.hpp>
30 using namespace boost::multi_index;
32 /* Measurement harness by Andrew Koenig, extracted from companion code to
33 * Stroustrup, B.: "Wrapping C++ Member Function Calls", The C++ Report,
34 * June 2000, Vol 12/No 6.
35 * Original code retrievable at: http://www.research.att.com/~bs/wrap_code.cpp
38 // How many clock units does it take to interrogate the clock?
39 static double clock_overhead()
41 clock_t k = clock(), start, limit;
43 // Wait for the clock to tick
47 // interrogate the clock until it has advanced at least a second
48 // (for reasonable accuracy)
49 limit = start + CLOCKS_PER_SEC;
52 while ((k = clock()) < limit)
55 return double(k - start) / r;
58 // We'd like the odds to be factor:1 that the result is
59 // within percent% of the median
60 const int factor = 10;
61 const int percent = 20;
63 // Measure a function (object) factor*2 times,
64 // appending the measurements to the second argument
66 void measure_aux(F f, vector<double>& mv)
68 static double ovhd = clock_overhead();
70 // Ensure we don't reallocate in mid-measurement
71 mv.reserve(mv.size() + factor*2);
73 // Wait for the clock to tick
80 // Do 2*factor measurements
81 for (int i = 2*factor; i; --i) {
82 unsigned long count = 0, limit = 1, tcount = 0;
84 // Original code used CLOCKS_PER_SEC/100
85 const clock_t clocklimit = start + CLOCKS_PER_SEC/10;
89 while (count < limit) {
95 } while ((t = clock()) < clocklimit);
97 // Wait for the clock to tick again;
100 while ((t2 = clock()) == t);
102 // Append the measurement to the vector
103 mv.push_back(((t2 - start) - (tcount * ovhd)) / count);
105 // Establish a new starting point
110 // Returns the number of clock units per iteration
111 // With odds of factor:1, the measurement is within percent% of
112 // the value returned, which is also the median of all measurements.
118 int n = 0; // iteration counter
122 // Try 2*factor measurements
124 assert(mv.size() == 2*n*factor);
126 // Compute the median. We know the size is even, so we cheat.
127 sort(mv.begin(), mv.end());
128 double median = (mv[n*factor] + mv[n*factor-1])/2;
130 // If the extrema are within threshold of the median, we're done
131 if (mv[n] > (median * (100-percent))/100 &&
132 mv[mv.size() - n - 1] < (median * (100+percent))/100)
135 } while (mv.size() < factor * 200);
142 /* dereferencing compare predicate */
144 template <typename Iterator,typename Compare>
147 bool operator()(const Iterator& x,const Iterator& y)const{return comp(*x,*y);}
153 /* list_wrapper and multiset_wrapper adapt std::lists and std::multisets
154 * to make them conform to a set-like insert interface which test
155 * routines do assume.
158 template <typename List>
159 struct list_wrapper:List
161 typedef typename List::value_type value_type;
162 typedef typename List::iterator iterator;
164 pair<iterator,bool> insert(const value_type& v)
167 return pair<iterator,bool>(boost::prior(List::end()),true);
171 template <typename Multiset>
172 struct multiset_wrapper:Multiset
174 typedef typename Multiset::value_type value_type;
175 typedef typename Multiset::iterator iterator;
177 pair<iterator,bool> insert(const value_type& v)
179 return pair<iterator,bool>(Multiset::insert(v),true);
183 /* space comsumption of manual simulations is determined by checking
184 * the node sizes of the containers involved. This cannot be done in a
185 * portable manner, so node_size has to be written on a per stdlibrary
186 * basis. Add your own versions if necessary.
189 #if defined(BOOST_DINKUMWARE_STDLIB)
191 template<typename Container>
192 size_t node_size(const Container&)
194 return sizeof(*Container().begin()._Mynode());
197 #elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
199 template<typename Container>
200 size_t node_size(const Container&)
202 typedef typename Container::iterator::_Link_type node_ptr;
207 template<typename Value,typename Allocator>
208 size_t node_size(const list<Value,Allocator>&)
210 return sizeof(typename list<Value,Allocator>::iterator::_Node);
213 template<typename List>
214 size_t node_size(const list_wrapper<List>&)
216 return sizeof(typename List::iterator::_Node);
221 /* default version returns 0 by convention */
223 template<typename Container>
224 size_t node_size(const Container&)
231 /* mono_container runs the tested routine on multi_index and manual
232 * simulations comprised of one standard container.
233 * bi_container and tri_container run the equivalent routine for manual
234 * compositions of two and three standard containers, respectively.
237 template <typename Container>
238 struct mono_container
240 mono_container(int n_):n(n_){}
244 typedef typename Container::iterator iterator;
248 for(int i=0;i<n;++i)c.insert(i);
249 for(iterator it=c.begin();it!=c.end();)c.erase(it++);
252 static size_t multi_index_node_size()
254 return sizeof(*Container().begin().get_node());
257 static size_t node_size()
259 return ::node_size(Container());
266 template <typename Container1,typename Container2>
269 bi_container(int n_):n(n_){}
273 typedef typename Container1::iterator iterator1;
274 typedef typename Container2::iterator iterator2;
279 for(int i=0;i<n;++i){
280 iterator1 it1=c1.insert(i).first;
283 for(iterator2 it2=c2.begin();it2!=c2.end();)
290 static size_t node_size()
292 return ::node_size(Container1())+::node_size(Container2());
299 template <typename Container1,typename Container2,typename Container3>
302 tri_container(int n_):n(n_){}
306 typedef typename Container1::iterator iterator1;
307 typedef typename Container2::iterator iterator2;
308 typedef typename Container3::iterator iterator3;
314 for(int i=0;i<n;++i){
315 iterator1 it1=c1.insert(i).first;
316 iterator2 it2=c2.insert(it1).first;
319 for(iterator3 it3=c3.begin();it3!=c3.end();)
327 static size_t node_size()
329 return ::node_size(Container1())+
330 ::node_size(Container2())+::node_size(Container3());
337 /* measure and compare two routines for several numbers of elements
338 * and also estimates relative memory consumption.
341 template <typename IndexedTest,typename ManualTest>
342 void run_tests(const char* title)
344 cout<<fixed<<setprecision(2);
347 for(int i=0;i<3;++i){
348 double indexed_t=measure(IndexedTest(n));
349 double manual_t=measure(ManualTest(n));
350 cout<<" 10^"<<i+3<<" elmts: "
351 <<setw(6)<<100.0*indexed_t/manual_t<<"% "
353 <<setw(6)<<1000.0*indexed_t/CLOCKS_PER_SEC<<" ms / "
354 <<setw(6)<<1000.0*manual_t/CLOCKS_PER_SEC<<" ms)"
359 size_t indexed_t_node_size=IndexedTest::multi_index_node_size();
360 size_t manual_t_node_size=ManualTest::node_size();
362 if(manual_t_node_size){
363 cout<<" space gain: "
364 <<setw(6)<<100.0*indexed_t_node_size/manual_t_node_size<<"%"<<endl;
368 /* compare_structures accept a multi_index_container instantiation and
369 * several standard containers, builds a manual simulation out of the
370 * latter and run the tests.
373 template <typename IndexedType,typename ManualType>
374 void compare_structures(const char* title)
377 mono_container<IndexedType>,
378 mono_container<ManualType>
382 template <typename IndexedType,typename ManualType1,typename ManualType2>
383 void compare_structures2(const char* title)
386 mono_container<IndexedType>,
387 bi_container<ManualType1,ManualType2>
392 typename IndexedType,
393 typename ManualType1,typename ManualType2,typename ManualType3
395 void compare_structures3(const char* title)
398 mono_container<IndexedType>,
399 tri_container<ManualType1,ManualType2,ManualType3>
405 /* some stdlibs provide the discussed but finally rejected std::identity */
406 using boost::multi_index::identity;
409 /* 1 ordered index */
411 typedef multi_index_container<int> indexed_t;
412 typedef set<int> manual_t;
414 compare_structures<indexed_t,manual_t>(
418 /* 1 sequenced index */
420 typedef list_wrapper<
421 multi_index_container<
423 indexed_by<sequenced<> >
426 typedef list_wrapper<list<int> > manual_t;
428 compare_structures<indexed_t,manual_t>(
429 "1 sequenced index");
432 /* 2 ordered indices */
434 typedef multi_index_container<
437 ordered_unique<identity<int> >,
438 ordered_non_unique<identity<int> >
441 typedef set<int> manual_t1;
446 manual_t1::key_compare
450 compare_structures2<indexed_t,manual_t1,manual_t2>(
451 "2 ordered indices");
454 /* 1 ordered index + 1 sequenced index */
456 typedef multi_index_container<
459 boost::multi_index::ordered_unique<identity<int> >,
463 typedef list_wrapper<
474 compare_structures2<indexed_t,manual_t1,manual_t2>(
475 "1 ordered index + 1 sequenced index");
478 /* 3 ordered indices */
480 typedef multi_index_container<
483 ordered_unique<identity<int> >,
484 ordered_non_unique<identity<int> >,
485 ordered_non_unique<identity<int> >
488 typedef set<int> manual_t1;
489 typedef multiset_wrapper<
494 manual_t1::key_compare
502 manual_t2::key_compare
506 compare_structures3<indexed_t,manual_t1,manual_t2,manual_t3>(
507 "3 ordered indices");
510 /* 2 ordered indices + 1 sequenced index */
512 typedef multi_index_container<
515 ordered_unique<identity<int> >,
516 ordered_non_unique<identity<int> >,
520 typedef list_wrapper<
523 typedef multiset_wrapper<
536 manual_t2::key_compare
540 compare_structures3<indexed_t,manual_t1,manual_t2,manual_t3>(
541 "2 ordered indices + 1 sequenced index");