]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/container/bench/bench_alloc_expand_fwd.cpp
4fc5c617c459fc8f5972bfe3144d70cb8aed94aa
1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
12 #pragma warning (disable : 4512)
13 #pragma warning (disable : 4267)
14 #pragma warning (disable : 4244)
17 #define BOOST_CONTAINER_VECTOR_ALLOC_STATS
19 #include <boost/container/allocator.hpp>
21 #include <boost/container/vector.hpp>
23 #include <memory> //std::allocator
24 #include <iostream> //std::cout, std::endl
25 #include <cstring> //std::strcmp
26 #include <boost/move/detail/nsec_clock.hpp>
28 using boost::move_detail::cpu_timer
;
29 using boost::move_detail::cpu_times
;
30 using boost::move_detail::nanosecond_type
;
32 namespace bc
= boost::container
;
34 #if defined(BOOST_CONTAINER_VECTOR_ALLOC_STATS)
36 template<class T
, class Allocator
>
37 static void reset_alloc_stats(std::vector
<T
, Allocator
> &)
40 template<class T
, class Allocator
>
41 static std::size_t get_num_alloc(std::vector
<T
, Allocator
> &)
44 template<class T
, class Allocator
>
45 static std::size_t get_num_expand(std::vector
<T
, Allocator
> &)
48 template<class T
, class Allocator
>
49 static void reset_alloc_stats(bc::vector
<T
, Allocator
> &v
)
50 { v
.reset_alloc_stats(); }
52 template<class T
, class Allocator
>
53 static std::size_t get_num_alloc(bc::vector
<T
, Allocator
> &v
)
54 { return v
.num_alloc
; }
56 template<class T
, class Allocator
>
57 static std::size_t get_num_expand(bc::vector
<T
, Allocator
> &v
)
58 { return v
.num_expand_fwd
; }
60 #endif //BOOST_CONTAINER_VECTOR_ALLOC_STATS
67 explicit MyInt(int i
= 0)
71 MyInt(const MyInt
&other
)
75 MyInt
& operator=(const MyInt
&other
)
87 template<class Container
>
88 void vector_test_template(unsigned int num_iterations
, unsigned int num_elements
)
90 unsigned int numalloc
= 0, numexpand
= 0;
95 unsigned int capacity
= 0;
96 for(unsigned int r
= 0; r
!= num_iterations
; ++r
){
98 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
101 //v.reserve(num_elements);
104 for(unsigned int e = 0; e != num_elements/3; ++e){
105 v.insert(v.end(), &a[0], &a[0]+3);
108 for(unsigned int e = 0; e != num_elements/3; ++e){
109 v.insert(v.end(), 3, MyInt(e));
112 for(unsigned int e = 0; e != num_elements/3; ++e){
113 v.insert(v.empty() ? v.end() : --v.end(), &a[0], &a[0]+3);
116 for(unsigned int e = 0; e != num_elements/3; ++e){
117 v.insert(v.empty() ? v.end() : --v.end(), 3, MyInt(e));
120 for(unsigned int e = 0; e != num_elements/3; ++e){
121 v.insert(v.size() >= 3 ? v.end()-3 : v.begin(), &a[0], &a[0]+3);
124 for(unsigned int e = 0; e != num_elements/3; ++e){
125 v.insert(v.size() >= 3 ? v.end()-3 : v.begin(), 3, MyInt(e));
128 for(unsigned int e = 0; e != num_elements; ++e){
129 v.insert(v.end(), MyInt(e));
132 for(unsigned int e = 0; e != num_elements; ++e){
133 v.insert(v.empty() ? v.end() : --v.end(), MyInt(e));
136 for(unsigned int e
= 0; e
!= num_elements
; ++e
){
137 v
.push_back(MyInt(e
));
140 #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
141 numalloc
+= get_num_alloc(v
);
142 numexpand
+= get_num_expand(v
);
144 capacity
= static_cast<unsigned int>(v
.capacity());
148 nanosecond_type nseconds
= timer
.elapsed().wall
;
150 std::cout
<< std::endl
151 << "Allocator: " << typeid(typename
Container::allocator_type
).name()
154 << float(nseconds
)/(num_iterations
*num_elements
)
156 << " capacity - alloc calls (new/expand): "
157 << (unsigned int)capacity
<< " - "
158 << (float(numalloc
) + float(numexpand
))/num_iterations
159 << "(" << float(numalloc
)/num_iterations
<< "/" << float(numexpand
)/num_iterations
<< ")"
160 << std::endl
<< std::endl
;
161 bc::dlmalloc_trim(0);
166 std::cout
<< "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
167 << "Capacity" << ";" << "push_back(ns)" << ";" << "Allocator calls" << ";"
168 << "New allocations" << ";" << "Fwd expansions" << std::endl
;
173 //#define SINGLE_TEST
177 std::size_t numit
[] = { 1000 };
179 std::size_t numit
[] = { 100 };
181 std::size_t numele
[] = { 10000 };
182 #elif defined SIMPLE_IT
183 std::size_t numit
[] = { 3 };
184 std::size_t numele
[] = { 10000 };
187 unsigned int numit
[] = { 1000, 10000, 100000, 1000000 };
189 unsigned int numit
[] = { 100, 1000, 10000, 100000 };
191 unsigned int numele
[] = { 10000, 1000, 100, 10 };
195 for(unsigned int i
= 0; i
< sizeof(numele
)/sizeof(numele
[0]); ++i
){
196 vector_test_template
< bc::vector
<MyInt
, std::allocator
<MyInt
> > >(numit
[i
], numele
[i
]);
197 vector_test_template
< bc::vector
<MyInt
, bc::allocator
<MyInt
, 1> > >(numit
[i
], numele
[i
]);
198 vector_test_template
<bc::vector
<MyInt
, bc::allocator
<MyInt
, 2, bc::expand_bwd
| bc::expand_fwd
> > >(numit
[i
], numele
[i
]);
199 vector_test_template
<bc::vector
<MyInt
, bc::allocator
<MyInt
, 2> > >(numit
[i
], numele
[i
]);