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
|
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2007-2013. Distributed under 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)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
#pragma warning (disable : 4512)
#endif
#include <boost/container/allocator.hpp>
#define BOOST_CONTAINER_VECTOR_ALLOC_STATS
#include <boost/container/vector.hpp>
#undef BOOST_CONTAINER_VECTOR_ALLOC_STATS
#include <memory> //std::allocator
#include <iostream> //std::cout, std::endl
#include <cassert> //assert
#include <boost/move/detail/nsec_clock.hpp>
using boost::move_detail::cpu_timer;
using boost::move_detail::cpu_times;
using boost::move_detail::nanosecond_type;
namespace bc = boost::container;
class MyInt
{
std::ptrdiff_t int_; //Use a type that will grow on 64 bit machines
public:
MyInt(int i = 0) : int_(i){}
MyInt(const MyInt &other)
: int_(other.int_)
{}
MyInt & operator=(const MyInt &other)
{
int_ = other.int_;
return *this;
}
};
typedef std::allocator<MyInt> StdAllocator;
typedef bc::allocator<MyInt, 2> AllocatorPlusV2;
typedef bc::allocator<MyInt, 1> AllocatorPlusV1;
template<class Allocator> struct get_allocator_name;
template<> struct get_allocator_name<StdAllocator>
{ static const char *get() { return "StdAllocator"; } };
template<> struct get_allocator_name<AllocatorPlusV2>
{ static const char *get() { return "AllocatorPlusV2"; } };
template<> struct get_allocator_name<AllocatorPlusV1>
{ static const char *get() { return "AllocatorPlusV1"; } };
void print_header()
{
std::cout << "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
<< "num_shrink" << ";" << "shrink_to_fit(ns)" << std::endl;
}
template<class Allocator>
void vector_test_template(std::size_t num_iterations, std::size_t num_elements, bool csv_output)
{
typedef Allocator IntAllocator;
std::size_t capacity = 0;
const std::size_t Step = 5;
std::size_t num_shrink = 0;
(void)capacity;
cpu_timer timer;
timer.resume();
#ifndef NDEBUG
typedef bc::dtl::integral_constant
<unsigned, bc::dtl::version<Allocator>::value> alloc_version;
#endif
for(std::size_t r = 0; r != num_iterations; ++r){
bc::vector<MyInt, IntAllocator> v(num_elements);
v.reset_alloc_stats();
num_shrink = 0;
for(std::size_t e = num_elements; e != 0; e -= Step){
v.erase(v.end() - std::ptrdiff_t(Step), v.end());
v.shrink_to_fit();
assert( (alloc_version::value != 2) || (e == Step) || (v.num_shrink > num_shrink) );
num_shrink = v.num_shrink;
}
assert(v.empty());
assert(0 == v.capacity());
}
timer.stop();
nanosecond_type nseconds = timer.elapsed().wall;
if(csv_output){
std::cout << get_allocator_name<Allocator>::get()
<< ";"
<< num_iterations
<< ";"
<< num_elements
<< ";"
<< num_shrink
<< ";"
<< float(nseconds)/float(num_iterations*num_elements)
<< std::endl;
}
else{
std::cout << std::endl
<< "Allocator: " << get_allocator_name<Allocator>::get()
<< std::endl
<< " num_shrink: " << num_shrink
<< std::endl
<< " shrink_to_fit ns: "
<< float(nseconds)/float(num_iterations*num_elements)
<< std::endl << std::endl;
}
bc::dlmalloc_trim(0);
}
int main(int argc, const char *argv[])
{
//#define SINGLE_TEST
#define SIMPLE_IT
#ifdef SINGLE_TEST
#ifdef NDEBUG
std::size_t numit [] = { 10 };
#else
std::size_t numit [] = { 50 };
std::size_t numele[] = { 2000 };
#endif
#elif defined SIMPLE_IT
std::size_t numit [] = { 3 };
std::size_t numele[] = { 2000 };
#else
#ifdef NDEBUG
std::size_t numit [] = { 100, 1000, 10000 };
#else
std::size_t numit [] = { 10, 100, 1000 };
#endif
std::size_t numele [] = { 10000, 2000, 500 };
#endif
bool csv_output = argc == 2 && (strcmp(argv[1], "--csv-output") == 0);
if(csv_output){
print_header();
for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
vector_test_template<StdAllocator>(numit[i], numele[i], csv_output);
}
for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
vector_test_template<AllocatorPlusV1>(numit[i], numele[i], csv_output);
}
for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
vector_test_template<AllocatorPlusV2>(numit[i], numele[i], csv_output);
}
}
else{
for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
std::cout << "\n ----------------------------------- \n"
<< " Iterations/Elements: " << numit[i] << "/" << numele[i]
<< "\n ----------------------------------- \n";
vector_test_template<StdAllocator>(numit[i], numele[i], csv_output);
vector_test_template<AllocatorPlusV1>(numit[i], numele[i], csv_output);
vector_test_template<AllocatorPlusV2>(numit[i], numele[i], csv_output);
}
}
return 0;
}
|