]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/container/bench/bench_alloc_shrink_to_fit.cpp
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)
15 #include <boost/container/allocator.hpp>
17 #define BOOST_CONTAINER_VECTOR_ALLOC_STATS
19 #include <boost/container/vector.hpp>
21 #undef BOOST_CONTAINER_VECTOR_ALLOC_STATS
23 #include <memory> //std::allocator
24 #include <iostream> //std::cout, std::endl
25 #include <cassert> //assert
27 #include <boost/timer/timer.hpp>
28 using boost::timer::cpu_timer
;
29 using boost::timer::cpu_times
;
30 using boost::timer::nanosecond_type
;
32 namespace bc
= boost::container
;
34 typedef std::allocator
<int> StdAllocator
;
35 typedef bc::allocator
<int, 2> AllocatorPlusV2
;
36 typedef bc::allocator
<int, 1> AllocatorPlusV1
;
38 template<class Allocator
> struct get_allocator_name
;
40 template<> struct get_allocator_name
<StdAllocator
>
41 { static const char *get() { return "StdAllocator"; } };
43 template<> struct get_allocator_name
<AllocatorPlusV2
>
44 { static const char *get() { return "AllocatorPlusV2"; } };
46 template<> struct get_allocator_name
<AllocatorPlusV1
>
47 { static const char *get() { return "AllocatorPlusV1"; } };
51 std::size_t int_
; //Use a type that will grow on 64 bit machines
54 MyInt(int i
= 0) : int_(i
){}
56 MyInt(const MyInt
&other
)
60 MyInt
& operator=(const MyInt
&other
)
69 std::cout
<< "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
70 << "num_shrink" << ";" << "shrink_to_fit(ns)" << std::endl
;
73 template<class Allocator
>
74 void vector_test_template(unsigned int num_iterations
, unsigned int num_elements
, bool csv_output
)
76 typedef typename
Allocator::template rebind
<MyInt
>::other IntAllocator
;
78 unsigned int capacity
= 0;
79 const std::size_t Step
= 5;
80 unsigned int num_shrink
= 0;
87 typedef bc::dtl::integral_constant
88 <unsigned, bc::dtl::version
<Allocator
>::value
> alloc_version
;
91 for(unsigned int r
= 0; r
!= num_iterations
; ++r
){
92 bc::vector
<MyInt
, IntAllocator
> v(num_elements
);
93 v
.reset_alloc_stats();
95 for(unsigned int e
= num_elements
; e
!= 0; e
-= Step
){
96 v
.erase(v
.end() - Step
, v
.end());
98 assert( (alloc_version::value
!= 2) || (e
== Step
) || (v
.num_shrink
> num_shrink
) );
99 num_shrink
= v
.num_shrink
;
102 assert(0 == v
.capacity());
106 nanosecond_type nseconds
= timer
.elapsed().wall
;
109 std::cout
<< get_allocator_name
<Allocator
>::get()
117 << float(nseconds
)/(num_iterations
*num_elements
)
121 std::cout
<< std::endl
122 << "Allocator: " << get_allocator_name
<Allocator
>::get()
124 << " num_shrink: " << num_shrink
126 << " shrink_to_fit ns: "
127 << float(nseconds
)/(num_iterations
*num_elements
)
128 << std::endl
<< std::endl
;
130 bc::dlmalloc_trim(0);
133 int main(int argc
, const char *argv
[])
138 unsigned int numit
[] = { 100, 1000, 10000 };
140 unsigned int numit
[] = { 10, 100, 1000 };
142 unsigned int numele
[] = { 10000, 2000, 500 };
145 unsigned int numit
[] = { 500 };
147 unsigned int numit
[] = { 50 };
149 unsigned int numele
[] = { 2000 };
152 bool csv_output
= argc
== 2 && (strcmp(argv
[1], "--csv-output") == 0);
156 for(unsigned int i
= 0; i
< sizeof(numele
)/sizeof(numele
[0]); ++i
){
157 vector_test_template
<StdAllocator
>(numit
[i
], numele
[i
], csv_output
);
159 for(unsigned int i
= 0; i
< sizeof(numele
)/sizeof(numele
[0]); ++i
){
160 vector_test_template
<AllocatorPlusV1
>(numit
[i
], numele
[i
], csv_output
);
162 for(unsigned int i
= 0; i
< sizeof(numele
)/sizeof(numele
[0]); ++i
){
163 vector_test_template
<AllocatorPlusV2
>(numit
[i
], numele
[i
], csv_output
);
167 for(unsigned int i
= 0; i
< sizeof(numele
)/sizeof(numele
[0]); ++i
){
168 std::cout
<< "\n ----------------------------------- \n"
169 << " Iterations/Elements: " << numit
[i
] << "/" << numele
[i
]
170 << "\n ----------------------------------- \n";
171 vector_test_template
<StdAllocator
>(numit
[i
], numele
[i
], csv_output
);
172 vector_test_template
<AllocatorPlusV1
>(numit
[i
], numele
[i
], csv_output
);
173 vector_test_template
<AllocatorPlusV2
>(numit
[i
], numele
[i
], csv_output
);