1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2013
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
14 #include <boost/intrusive/detail/config_begin.hpp>
15 #include <boost/config.hpp>
18 #include <boost/intrusive/set.hpp>
19 #include <boost/intrusive/sg_set.hpp>
20 #include <boost/intrusive/avl_set.hpp>
21 #include <boost/date_time/posix_time/posix_time.hpp>
24 using namespace boost::posix_time
;
25 using namespace boost::intrusive
;
27 template<bool BigSize
> struct filler
{ int dummy
[10]; };
28 template <> struct filler
<false> {};
30 template<bool BigSize
> //The object for non-intrusive containers
31 struct test_class
: private filler
<BigSize
>
34 friend bool operator <(const test_class
&l
, const test_class
&r
) { return l
.i_
< r
.i_
; }
35 friend bool operator >(const test_class
&l
, const test_class
&r
) { return l
.i_
> r
.i_
; }
38 template <bool BigSize
, class HookType
>
39 struct itest_class
//The object for intrusive containers
40 : public HookType
, public test_class
<BigSize
>
45 const std::size_t NumElem
= 1000000;
47 const std::size_t NumElem
= 10000;
49 const std::size_t NumRepeat
= 4;
57 template<class Container
>
58 void fill_vector(Container
&values
, InsertionType insertion_type
)
60 switch(insertion_type
){
62 for( typename
Container::size_type i
= 0, max
= values
.size()
71 for( typename
Container::size_type i
= 0, max
= values
.size()
76 std::random_shuffle(values
.begin(), values
.end());
85 template<class Container
>
86 void test_insertion(Container
&c
, const char *ContainerName
, InsertionType insertion_type
)
88 std::cout
<< "Container " << ContainerName
<< std::endl
;
90 typedef typename
Container::size_type size_type
;
91 typedef typename
Container::value_type value_type
;
93 std::vector
<value_type
> values(NumElem
);
95 fill_vector(values
, insertion_type
);
97 tini
= microsec_clock::universal_time();
98 for( size_type repeat
= 0, repeat_max
= NumRepeat
99 ; repeat
!= repeat_max
102 for( size_type i
= 0, max
= values
.size()
107 if(c
.size() != values
.size()){
108 std::cout
<< " ERROR: size not consistent" << std::endl
;
111 tend
= microsec_clock::universal_time();
112 std::cout
<< " Insert ns/iter: " << double((tend
-tini
).total_nanoseconds())/double(NumElem
*NumRepeat
) << std::endl
;
117 tini
= microsec_clock::universal_time();
118 for( size_type repeat
= 0, repeat_max
= NumRepeat
119 ; repeat
!= repeat_max
122 for( size_type i
= 0, max
= values
.size()
126 found
+= static_cast<size_type
>(c
.end() != c
.find(v
));
128 if(found
!= NumElem
){
129 std::cout
<< " ERROR: all not found (" << found
<< ") vs. (" << NumElem
<< ")" << std::endl
;
132 tend
= microsec_clock::universal_time();
133 std::cout
<< " Search ns/iter: " << double((tend
-tini
).total_nanoseconds())/double(NumElem
*NumRepeat
) << std::endl
;
138 void test_insert_search(InsertionType insertion_type
)
141 typedef set_base_hook
< link_mode
<normal_link
> > SetHook
;
142 typedef set
< itest_class
<true, SetHook
> > Set
;
144 test_insertion(c
, "Set", insertion_type
);
147 typedef avl_set_base_hook
< link_mode
<normal_link
> > AvlSetHook
;
148 typedef avl_set
< itest_class
<true, AvlSetHook
> > AvlSet
;
150 test_insertion(c
, "AvlSet", insertion_type
);
153 typedef bs_set_base_hook
< link_mode
<normal_link
> > BsSetHook
;
154 typedef sg_set
< itest_class
<true, BsSetHook
> > SgSet
;
156 c
.balance_factor(0.55f
);
157 test_insertion(c
, "SgSet(alpha 0.55)", insertion_type
);
160 typedef bs_set_base_hook
< link_mode
<normal_link
> > BsSetHook
;
161 typedef sg_set
< itest_class
<true, BsSetHook
> > SgSet
;
163 c
.balance_factor(0.60f
);
164 test_insertion(c
, "SgSet(alpha 0.60)", insertion_type
);
167 typedef bs_set_base_hook
< link_mode
<normal_link
> > BsSetHook
;
168 typedef sg_set
< itest_class
<true, BsSetHook
> > SgSet
;
170 c
.balance_factor(0.65f
);
171 test_insertion(c
, "SgSet(alpha 0.65)", insertion_type
);
174 typedef bs_set_base_hook
< link_mode
<normal_link
> > BsSetHook
;
175 typedef sg_set
< itest_class
<true, BsSetHook
> > SgSet
;
177 test_insertion(c
, "SgSet(alpha 0.7)", insertion_type
);
180 typedef bs_set_base_hook
< link_mode
<normal_link
> > BsSetHook
;
181 typedef sg_set
< itest_class
<true, BsSetHook
> > SgSet
;
183 c
.balance_factor(0.75f
);
184 test_insertion(c
, "SgSet(alpha 0.75)", insertion_type
);
187 typedef bs_set_base_hook
< link_mode
<normal_link
> > BsSetHook
;
188 typedef sg_set
< itest_class
<true, BsSetHook
> > SgSet
;
190 c
.balance_factor(0.80f
);
191 test_insertion(c
, "SgSet(alpha 0.80)", insertion_type
);
194 typedef bs_set_base_hook
< link_mode
<normal_link
> > BsSetHook
;
195 typedef sg_set
< itest_class
<true, BsSetHook
>, floating_point
<false> > SgSet
;
197 test_insertion(c
, "SgSet(no float, alpha 1/sqrt(2)~0,7071)", insertion_type
);
203 std::cout
<< "MONOTONIC INPUTS\n";
204 std::cout
<< "----------------\n\n";
205 test_insert_search(Monotonic
);
206 std::cout
<< "----------------\n\n";
207 std::cout
<< "RANDOM INPUTS\n";
208 std::cout
<< "----------------\n\n";
209 test_insert_search(Random
);
210 std::cout
<< "----------------\n\n";
214 #include <boost/intrusive/detail/config_end.hpp>