Change allocator name logic

This commit is contained in:
Ion Gaztañaga
2017-10-20 00:31:53 +02:00
parent 2a7b5a70a5
commit 44919284a9

View File

@@ -24,81 +24,41 @@
#include <iostream> //std::cout, std::endl #include <iostream> //std::cout, std::endl
#include <cstring> //std::strcmp #include <cstring> //std::strcmp
#include <boost/timer/timer.hpp> #include <boost/timer/timer.hpp>
#include <typeinfo>
using boost::timer::cpu_timer; using boost::timer::cpu_timer;
using boost::timer::cpu_times; using boost::timer::cpu_times;
using boost::timer::nanosecond_type; using boost::timer::nanosecond_type;
namespace bc = boost::container; namespace bc = boost::container;
typedef std::allocator<int> StdAllocator;
typedef bc::allocator<int, 2, bc::expand_bwd | bc::expand_fwd> AllocatorPlusV2Mask;
typedef bc::allocator<int, 2> AllocatorPlusV2;
typedef bc::allocator<int, 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<AllocatorPlusV2Mask>
{ static const char *get() { return "AllocatorPlusV2Mask"; } };
template<> struct get_allocator_name<AllocatorPlusV2>
{ static const char *get() { return "AllocatorPlusV2"; } };
template<> struct get_allocator_name<AllocatorPlusV1>
{ static const char *get() { return "AllocatorPlusV1"; } };
#if defined(BOOST_CONTAINER_VECTOR_ALLOC_STATS) #if defined(BOOST_CONTAINER_VECTOR_ALLOC_STATS)
//
// stats_traits;
//
template<template<class, class> class Vector> template<class T, class Allocator>
struct stats_traits; static void reset_alloc_stats(std::vector<T, Allocator> &)
{}
template<> template<class T, class Allocator>
struct stats_traits<std::vector> static std::size_t get_num_alloc(std::vector<T, Allocator> &)
{ { return 0; }
template<class T, class Allocator>
static void reset_alloc_stats(std::vector<T, Allocator> &)
{}
template<class T, class Allocator> template<class T, class Allocator>
static std::size_t get_num_alloc(std::vector<T, Allocator> &) static std::size_t get_num_expand(std::vector<T, Allocator> &)
{ return 0; } { return 0; }
template<class T, class Allocator> template<class T, class Allocator>
static std::size_t get_num_expand(std::vector<T, Allocator> &) static void reset_alloc_stats(bc::vector<T, Allocator> &v)
{ return 0; } { v.reset_alloc_stats(); }
};
template<> template<class T, class Allocator>
struct stats_traits<bc::vector> static std::size_t get_num_alloc(bc::vector<T, Allocator> &v)
{ { return v.num_alloc; }
template<class T, class Allocator>
static void reset_alloc_stats(bc::vector<T, Allocator> &v)
{ v.reset_alloc_stats(); }
template<class T, class Allocator> template<class T, class Allocator>
static std::size_t get_num_alloc(bc::vector<T, Allocator> &v) static std::size_t get_num_expand(bc::vector<T, Allocator> &v)
{ return v.num_alloc; } { return v.num_expand_fwd; }
template<class T, class Allocator>
static std::size_t get_num_expand(bc::vector<T, Allocator> &v)
{ return v.num_expand_fwd; }
};
#endif //BOOST_CONTAINER_VECTOR_ALLOC_STATS #endif //BOOST_CONTAINER_VECTOR_ALLOC_STATS
template<template<class, class> class Vector> struct get_container_name;
template<> struct get_container_name<std::vector>
{ static const char *get() { return "StdVector"; } };
template<> struct get_container_name<bc::vector>
{ static const char *get() { return "BoostContainerVector"; } };
class MyInt class MyInt
{ {
int int_; int int_;
@@ -124,24 +84,19 @@ class MyInt
} }
}; };
template<class Allocator, template <class, class> class Vector> template<class Container>
void vector_test_template(unsigned int num_iterations, unsigned int num_elements, bool csv_output) void vector_test_template(unsigned int num_iterations, unsigned int num_elements)
{ {
typedef typename Allocator::template rebind<MyInt>::other IntAllocator;
unsigned int numalloc = 0, numexpand = 0; unsigned int numalloc = 0, numexpand = 0;
#ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
typedef stats_traits<Vector> stats_traits_t;
#endif
cpu_timer timer; cpu_timer timer;
timer.resume(); timer.resume();
unsigned int capacity = 0; unsigned int capacity = 0;
for(unsigned int r = 0; r != num_iterations; ++r){ for(unsigned int r = 0; r != num_iterations; ++r){
Vector<MyInt, IntAllocator> v; Container v;
#ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
stats_traits_t::reset_alloc_stats(v); reset_alloc_stats(v);
#endif #endif
//v.reserve(num_elements); //v.reserve(num_elements);
//MyInt a[3]; //MyInt a[3];
@@ -183,8 +138,8 @@ void vector_test_template(unsigned int num_iterations, unsigned int num_elements
} }
#ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS #ifdef BOOST_CONTAINER_VECTOR_ALLOC_STATS
numalloc += stats_traits_t::get_num_alloc(v); numalloc += get_num_alloc(v);
numexpand += stats_traits_t::get_num_expand(v); numexpand += get_num_expand(v);
#endif #endif
capacity = static_cast<unsigned int>(v.capacity()); capacity = static_cast<unsigned int>(v.capacity());
} }
@@ -192,37 +147,17 @@ void vector_test_template(unsigned int num_iterations, unsigned int num_elements
timer.stop(); timer.stop();
nanosecond_type nseconds = timer.elapsed().wall; nanosecond_type nseconds = timer.elapsed().wall;
if(csv_output){ std::cout << std::endl
std::cout << get_allocator_name<Allocator>::get() << "Allocator: " << typeid(typename Container::allocator_type).name()
<< ";" << std::endl
<< num_iterations << " push_back ns: "
<< ";" << float(nseconds)/(num_iterations*num_elements)
<< num_elements << std::endl
<< ";" << " capacity - alloc calls (new/expand): "
<< capacity << (unsigned int)capacity << " - "
<< ";"
<< float(nseconds)/(num_iterations*num_elements)
<< ";"
<< (float(numalloc) + float(numexpand))/num_iterations << (float(numalloc) + float(numexpand))/num_iterations
<< ";" << "(" << float(numalloc)/num_iterations << "/" << float(numexpand)/num_iterations << ")"
<< float(numalloc)/num_iterations << std::endl << std::endl;
<< ";"
<< float(numexpand)/num_iterations
<< std::endl;
}
else{
std::cout << std::endl
<< "Allocator: " << get_allocator_name<Allocator>::get()
<< std::endl
<< " push_back ns: "
<< float(nseconds)/(num_iterations*num_elements)
<< std::endl
<< " capacity - alloc calls (new/expand): "
<< (unsigned int)capacity << " - "
<< (float(numalloc) + float(numexpand))/num_iterations
<< "(" << float(numalloc)/num_iterations << "/" << float(numexpand)/num_iterations << ")"
<< std::endl << std::endl;
}
bc::dlmalloc_trim(0); bc::dlmalloc_trim(0);
} }
@@ -233,7 +168,7 @@ void print_header()
<< "New allocations" << ";" << "Fwd expansions" << std::endl; << "New allocations" << ";" << "Fwd expansions" << std::endl;
} }
int main(int argc, const char *argv[]) int main()
{ {
#define SINGLE_TEST #define SINGLE_TEST
#ifndef SINGLE_TEST #ifndef SINGLE_TEST
@@ -252,34 +187,12 @@ int main(int argc, const char *argv[])
std::size_t numele [] = { 10000 }; std::size_t numele [] = { 10000 };
#endif #endif
bool csv_output = argc == 2 && (strcmp(argv[1], "--csv-output") == 0); print_header();
for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
if(csv_output){ vector_test_template< bc::vector<MyInt, std::allocator<MyInt> > >(numit[i], numele[i]);
print_header(); vector_test_template< bc::vector<MyInt, bc::allocator<MyInt, 1> > >(numit[i], numele[i]);
for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){ vector_test_template<bc::vector<MyInt, bc::allocator<MyInt, 2, bc::expand_bwd | bc::expand_fwd> > >(numit[i], numele[i]);
vector_test_template<StdAllocator, bc::vector>(numit[i], numele[i], csv_output); vector_test_template<bc::vector<MyInt, bc::allocator<MyInt, 2> > >(numit[i], numele[i]);
}
for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
vector_test_template<AllocatorPlusV1, bc::vector>(numit[i], numele[i], csv_output);
}
for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
vector_test_template<AllocatorPlusV2Mask, bc::vector>(numit[i], numele[i], csv_output);
}
for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
vector_test_template<AllocatorPlusV2, bc::vector>(numit[i], numele[i], csv_output);
}
}
else{
for(unsigned int 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, std::vector>(numit[i], numele[i], csv_output);
vector_test_template<StdAllocator, bc::vector>(numit[i], numele[i], csv_output);
vector_test_template<AllocatorPlusV1, bc::vector>(numit[i], numele[i], csv_output);
vector_test_template<AllocatorPlusV2Mask, bc::vector>(numit[i], numele[i], csv_output);
vector_test_template<AllocatorPlusV2, bc::vector>(numit[i], numele[i], csv_output);
}
} }
return 0; return 0;
} }