Skip to content

Commit c774d39

Browse files
committed
Divide bench_vectors.cpp into several smaller benchmarks.
1 parent 3020328 commit c774d39

8 files changed

Lines changed: 383 additions & 168 deletions

bench/bench_deque.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//////////////////////////////////////////////////////////////////////////////
2+
//
3+
// (C) Copyright Ion Gaztanaga 2007-2026. 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)
6+
//
7+
// See http://www.boost.org/libs/container for documentation.
8+
//
9+
//////////////////////////////////////////////////////////////////////////////
10+
//
11+
// Compares boost::container::deque (reservable and non-reservable) against
12+
// std::deque.
13+
//
14+
//////////////////////////////////////////////////////////////////////////////
15+
16+
#include <deque>
17+
#include <memory> //std::allocator
18+
#include <boost/container/deque.hpp>
19+
#include <boost/container/options.hpp>
20+
21+
#include "bench_vector_common.hpp"
22+
23+
template<class IntType, class Operation>
24+
void run_containers(unsigned numit, unsigned numele, bool bp)
25+
{
26+
vector_test_template< std::deque<IntType, std::allocator<IntType> >, Operation >(numit, numele, "std::deque ", bp);
27+
vector_test_template< bc::deque<IntType, std::allocator<IntType> >, Operation >(numit, numele, "deque ", bp);
28+
vector_test_template< bc::deque<IntType, std::allocator<IntType>,
29+
typename bc::deque_options<bc::reservable<true> >::type >, Operation >(numit, numele, "deque(reserv) ", bp);
30+
}
31+
32+
int main()
33+
{
34+
test_vectors<int>();
35+
return 0;
36+
}

bench/bench_devector.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//////////////////////////////////////////////////////////////////////////////
2+
//
3+
// (C) Copyright Ion Gaztanaga 2007-2026. 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)
6+
//
7+
// See http://www.boost.org/libs/container for documentation.
8+
//
9+
//////////////////////////////////////////////////////////////////////////////
10+
//
11+
// Compares boost::container::devector against boost::container::deque and
12+
// boost::container::vector.
13+
//
14+
//////////////////////////////////////////////////////////////////////////////
15+
16+
#include <memory> //std::allocator
17+
#include <boost/container/devector.hpp>
18+
#include <boost/container/deque.hpp>
19+
#include <boost/container/vector.hpp>
20+
21+
#include "bench_vector_common.hpp"
22+
23+
template<class IntType, class Operation>
24+
void run_containers(unsigned numit, unsigned numele, bool bp)
25+
{
26+
vector_test_template< bc::devector<IntType, std::allocator<IntType> >, Operation >(numit, numele, "devector ", bp);
27+
vector_test_template< bc::deque<IntType, std::allocator<IntType> >, Operation >(numit, numele, "deque ", bp);
28+
vector_test_template< bc::vector<IntType, std::allocator<IntType> >, Operation >(numit, numele, "vector ", bp);
29+
}
30+
31+
int main()
32+
{
33+
test_vectors<int>();
34+
return 0;
35+
}

bench/bench_segtor.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//////////////////////////////////////////////////////////////////////////////
2+
//
3+
// (C) Copyright Ion Gaztanaga 2007-2026. 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)
6+
//
7+
// See http://www.boost.org/libs/container for documentation.
8+
//
9+
//////////////////////////////////////////////////////////////////////////////
10+
//
11+
// Compares boost::container::segtor (reservable and non-reservable) against
12+
// boost::container::deque (reservable and non-reservable).
13+
//
14+
//////////////////////////////////////////////////////////////////////////////
15+
16+
#include <memory> //std::allocator
17+
#include <boost/container/deque.hpp>
18+
#include <boost/container/segtor.hpp>
19+
#include <boost/container/options.hpp>
20+
21+
#include "bench_vector_common.hpp"
22+
23+
template<class IntType, class Operation>
24+
void run_containers(unsigned numit, unsigned numele, bool bp)
25+
{
26+
vector_test_template< bc::deque<IntType, std::allocator<IntType> >, Operation >(numit, numele, "deque ", bp);
27+
vector_test_template< bc::deque<IntType, std::allocator<IntType>,
28+
typename bc::deque_options<bc::reservable<true> >::type >, Operation >(numit, numele, "deque(reserv) ", bp);
29+
vector_test_template< bc::segtor<IntType, std::allocator<IntType> >, Operation >(numit, numele, "segtor ", bp);
30+
vector_test_template< bc::segtor<IntType, std::allocator<IntType>,
31+
typename bc::segtor_options<bc::reservable<true> >::type >, Operation >(numit, numele, "segtor(reserv) ", bp);
32+
}
33+
34+
int main()
35+
{
36+
test_vectors<int>();
37+
return 0;
38+
}

bench/bench_small_vector.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//////////////////////////////////////////////////////////////////////////////
2+
//
3+
// (C) Copyright Ion Gaztanaga 2007-2026. 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)
6+
//
7+
// See http://www.boost.org/libs/container for documentation.
8+
//
9+
//////////////////////////////////////////////////////////////////////////////
10+
//
11+
// Compares boost::container::small_vector against std::vector and
12+
// boost::container::vector.
13+
//
14+
//////////////////////////////////////////////////////////////////////////////
15+
16+
#include <vector>
17+
#include <memory> //std::allocator
18+
#include <boost/container/vector.hpp>
19+
#include <boost/container/small_vector.hpp>
20+
21+
#include "bench_vector_common.hpp"
22+
23+
template<class IntType, class Operation>
24+
void run_containers(unsigned numit, unsigned numele, bool bp)
25+
{
26+
vector_test_template< bc::small_vector<IntType, 0, std::allocator<IntType> >, Operation >(numit, numele, "small_vector ", bp);
27+
vector_test_template< std::vector<IntType, std::allocator<IntType> >, Operation >(numit, numele, "std::vector ", bp);
28+
vector_test_template< bc::vector<IntType, std::allocator<IntType> >, Operation >(numit, numele, "vector ", bp);
29+
}
30+
31+
int main()
32+
{
33+
test_vectors<int>();
34+
return 0;
35+
}

bench/bench_static_vector.cpp

Lines changed: 21 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,37 @@
1-
// benchmark based on: http://cpp-next.com/archive/2010/10/howards-stl-move-semantics-benchmark/
1+
//////////////////////////////////////////////////////////////////////////////
22
//
3-
// @file bench_static_vector.cpp
4-
// @date Aug 14, 2011
5-
// @author Andrew Hundt <ATHundt@gmail.com>
3+
// (C) Copyright Ion Gaztanaga 2007-2026. 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)
66
//
7-
// (C) Copyright 2011-2013 Andrew Hundt <ATHundt@gmail.com>
8-
// (C) Copyright 2013-2013 Ion Gaztanaga
7+
// See http://www.boost.org/libs/container for documentation.
98
//
10-
// Distributed under the Boost Software License, Version 1.0. (See
11-
// accompanying file LICENSE_1_0.txt or copy at
12-
// http://www.boost.org/LICENSE_1_0.txt)
9+
//////////////////////////////////////////////////////////////////////////////
1310
//
14-
// @brief varray_benchmark.cpp compares the performance of boost::container::varray to boost::container::vector
11+
// Compares boost::container::static_vector against std::vector and
12+
// boost::container::vector.
13+
//
14+
//////////////////////////////////////////////////////////////////////////////
1515

16-
#include "varray.hpp"
16+
#include <vector>
17+
#include <memory> //std::allocator
1718
#include <boost/container/vector.hpp>
1819
#include <boost/container/static_vector.hpp>
1920

20-
#include "../test/movable_int.hpp"
21-
#include <vector>
22-
#include <iostream>
23-
#include <boost/move/detail/nsec_clock.hpp>
24-
#include <algorithm>
25-
#include <exception>
26-
#include <iomanip>
27-
28-
using boost::move_detail::cpu_timer;
29-
using boost::move_detail::cpu_times;
30-
using boost::move_detail::nanosecond_type;
31-
32-
static const std::size_t N = 500;
33-
34-
#ifdef NDEBUG
35-
static const std::size_t Iter = 50;
36-
#else
37-
static const std::size_t Iter = 5;
38-
#endif
39-
40-
//#define BENCH_SIMPLE_CONSTRUCTION
41-
//#define BENCH_TRIVIAL_TYPE
21+
#include "bench_vector_common.hpp"
4222

43-
#ifdef BENCH_TRIVIAL_TYPE
44-
typedef std::size_t basic_type_t;
45-
#else
46-
typedef boost::container::test::copyable_int basic_type_t;
47-
#endif
48-
49-
template<typename T>
50-
T &get_set(std::size_t)
51-
{
52-
#ifdef BENCH_SIMPLE_CONSTRUCTION
53-
T &t = *new T(N);
54-
for (std::size_t i = 0; i < N; ++i)
55-
t[i] = basic_type_t(std::rand());
56-
#else
57-
T &t = *new T;
58-
t.reserve(N);
59-
for (std::size_t i = 0; i < N; ++i)
60-
t.push_back(basic_type_t(std::rand()));
61-
#endif
62-
return t;
63-
}
64-
65-
template<typename T>
66-
T &generate()
23+
template<class IntType, class Operation>
24+
void run_containers(unsigned numit, unsigned numele, bool bp)
6725
{
68-
T &v = *new T;
69-
v.reserve(N);
70-
71-
for (std::size_t i = 0; i < N; ++i){
72-
typename T::reference r = get_set<typename T::value_type>(i);
73-
v.push_back(boost::move(r));
74-
delete &r;
75-
}
76-
return v;
77-
}
78-
79-
template<typename T>
80-
cpu_times time_it()
81-
{
82-
cpu_timer sortTime,rotateTime,destructionTime;
83-
sortTime.stop();rotateTime.stop();destructionTime.stop();
84-
cpu_timer totalTime, constructTime;
85-
std::srand (0);
86-
for(std::size_t i = 0; i< Iter; ++i){
87-
constructTime.resume();
88-
{
89-
T &v = generate<T>();
90-
constructTime.stop();
91-
sortTime.resume();
92-
std::sort(v.begin(), v.end());
93-
sortTime.stop();
94-
rotateTime.resume();
95-
std::rotate(v.begin(), v.begin() + std::ptrdiff_t(v.size()/2), v.end());
96-
rotateTime.stop();
97-
destructionTime.resume();
98-
delete &v;
99-
}
100-
destructionTime.stop();
101-
}
102-
totalTime.stop();
103-
std::cout << std::fixed << std::setw( 11 );
104-
std::cout << " construction took " << double(constructTime.elapsed().wall)/double(1000000000) << "s\n";
105-
std::cout << " sort took " << double(sortTime.elapsed().wall)/double(1000000000) << "s\n";
106-
std::cout << " rotate took " << double(rotateTime.elapsed().wall)/double(1000000000) << "s\n";
107-
std::cout << " destruction took " << double(destructionTime.elapsed().wall)/double(1000000000) << "s\n";
108-
std::cout << " Total time = " << double(totalTime.elapsed().wall)/double(1000000000) << "s\n";
109-
return totalTime.elapsed();
110-
}
111-
112-
void compare_times(cpu_times time_numerator, cpu_times time_denominator){
113-
std::cout
114-
<< "\n wall = " << ((double)time_numerator.wall/(double)time_denominator.wall) << "\n\n";
26+
//static_vector has a fixed capacity, so it must be sized for the largest
27+
//element count exercised by the harness.
28+
vector_test_template< bc::static_vector<IntType, bench_max_numele>, Operation >(numit, numele, "static_vector ", bp);
29+
vector_test_template< std::vector<IntType, std::allocator<IntType> >, Operation >(numit, numele, "std::vector ", bp);
30+
vector_test_template< bc::vector<IntType, std::allocator<IntType> >, Operation >(numit, numele, "vector ", bp);
11531
}
11632

11733
int main()
11834
{
119-
std::cout << "N = " << N << " Iter = " << Iter << "\n\n";
120-
121-
std::cout << "varray benchmark:" << std::endl;
122-
cpu_times time_varray = time_it<boost::container::varray<boost::container::varray<basic_type_t,N>,N > >();
123-
124-
std::cout << "boost::container::static_vector benchmark" << std::endl;
125-
cpu_times time_boost_static_vector = time_it<boost::container::static_vector<boost::container::static_vector<basic_type_t,N>,N > >();
126-
127-
std::cout << "boost::container::vector benchmark" << std::endl;
128-
cpu_times time_boost_vector = time_it<boost::container::vector<boost::container::vector<basic_type_t> > >();
129-
130-
std::cout << "std::vector benchmark" << std::endl;
131-
cpu_times time_standard_vector = time_it<std::vector<std::vector<basic_type_t> > >();
132-
133-
std::cout << "varray/boost::container::vector total time comparison:";
134-
compare_times(time_varray, time_boost_vector);
135-
136-
std::cout << "varray/boost::container::static_vector total time comparison:";
137-
compare_times(time_varray, time_boost_static_vector);
138-
139-
std::cout << "varray/std::vector total time comparison:";
140-
compare_times(time_varray,time_standard_vector);
141-
35+
test_vectors<int>();
14236
return 0;
14337
}

0 commit comments

Comments
 (0)