[ create a new paste ] login | about

Link: http://codepad.org/rii3BHly    [ raw code | fork ]

C++, pasted on Feb 19:
#include "myvector.hpp"
#include <cstddef>
#include <list>
#include <vector>
#include <deque>
#include <boost/container/vector.hpp>
#include <boost/container/deque.hpp>
#include <boost/double_ended/devector.hpp>
#include <boost/double_ended/batch_deque.hpp>
#include <benchmark/benchmark.h>

#define Sequence typename
#define Callable typename

struct basic_init_tag {
    typedef basic_init_tag type;
};
struct vector_init_tag : basic_init_tag {
    typedef vector_init_tag type;
};

template< Sequence Seq >
struct init_tag {
    typedef basic_init_tag type;
};

template <typename T, class Alloc >
struct init_tag< std::vector<T, Alloc> > : vector_init_tag {};

template <typename T, class Alloc >
struct init_tag< boost::container::vector<T, Alloc> > : vector_init_tag {};

template <typename T, class SmallBufferPolicy, class GrowthPolicy, class Alloc >
struct init_tag< boost::double_ended::devector<T, SmallBufferPolicy, GrowthPolicy, Alloc> > : vector_init_tag {};

template< Sequence Seq >
void initialize( Seq& s, size_t n, basic_init_tag ) {
}

template< Sequence Seq >
void initialize( Seq& s, size_t n, vector_init_tag ) {
    typedef typename Seq::value_type value_type;
    typedef typename Seq::size_type size_type;
    s.reserve( size_type( n ) );
    std::fill_n( s.data(), n, value_type( 0 ) );
}

template< Sequence Seq >
void initialize( Seq& s, size_t n ) {
    initialize( s, n, typename init_tag<Seq>::type() );
}

typedef unsigned test_type;
const int min_size = 0;
const int max_size = 100000000;

template< Sequence Seq >
void BM_push_back( benchmark::State& state ) {
    while ( state.KeepRunning() ) {
        state.PauseTiming();
        Seq data;
        initialize( data, state.range_x() );
        state.ResumeTiming();
        for ( int j = 0; j < state.range_x(); ++j ) {
            data.push_back( 1 );
        }
        const size_t items_processed = state.iterations() * state.range_x();
        state.SetItemsProcessed( items_processed );
    }
}

template< Sequence Seq >
void BM_push_back_reserved( benchmark::State& state ) {
    while ( state.KeepRunning() ) {
        state.PauseTiming();
        Seq data;
        initialize( data, state.range_x() );
        state.ResumeTiming();
        for ( int j = 0; j < state.range_x(); ++j ) {
            data.push_back_reserved( 1 );
        }
        const size_t items_processed = state.iterations() * state.range_x();
        state.SetItemsProcessed( items_processed );
    }
}

template< typename T >
using std_vector = std::vector<T>;

template< typename T >
using boost_devector = boost::double_ended::devector<T>;

template< typename T >
using boost_vector = boost::container::vector<T>;

BENCHMARK_TEMPLATE( BM_push_back, std_vector<test_type> )->Range( min_size, max_size );
BENCHMARK_TEMPLATE( BM_push_back_reserved, std_vector<test_type> )->Range( min_size, max_size );
BENCHMARK_TEMPLATE( BM_push_back, boost_devector<test_type> )->Range( min_size, max_size );
BENCHMARK_TEMPLATE( BM_push_back, boost_vector<test_type> )->Range( min_size, max_size );

BENCHMARK_MAIN()


Create a new paste based on this one


Comments: