File: bench_alloc_stable_vector_burst.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (293 lines) | stat: -rw-r--r-- 10,554 bytes parent folder | download | duplicates (11)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//////////////////////////////////////////////////////////////////////////////
//
// (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)
#pragma warning (disable : 4541)
#pragma warning (disable : 4673)
#pragma warning (disable : 4671)
#pragma warning (disable : 4244)
#endif

#include <memory>    //std::allocator
#include <iostream>  //std::cout, std::endl
#include <vector>    //std::vector
#include <cstddef>   //std::size_t
#include <cassert>   //assert

#include <boost/container/allocator.hpp>
#include <boost/container/adaptive_pool.hpp>
#include <boost/container/stable_vector.hpp>
#include <boost/container/vector.hpp>
#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
{
   int int_;

   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, 1> AllocatorPlusV1;
typedef bc::allocator<MyInt, 2> AllocatorPlusV2;
typedef bc::adaptive_pool
   < MyInt
   , bc::ADP_nodes_per_block
   , 0//bc::ADP_max_free_blocks
   , 2
   , 2>                       AdPool2PercentV2;

template<class Allocator> struct get_allocator_name;

template<> struct get_allocator_name<StdAllocator>
{  static const char *get() {  return "StdAllocator";  } };

template<> struct get_allocator_name<AllocatorPlusV1>
{  static const char *get() {  return "AllocatorPlusV1";  } };

template<> struct get_allocator_name<AllocatorPlusV2>
{  static const char *get() {  return "AllocatorPlusV2";  } };

template<> struct get_allocator_name<AdPool2PercentV2>
{  static const char *get() {  return "AdPool2PercentV2";  } };

template<class Allocator>
struct get_vector
{
   typedef bc::vector<MyInt, Allocator> type;
   static const char *vector_name()
   {
      return "vector<MyInt>";
   }
};

template<class Allocator>
struct get_stable_vector
{
   typedef bc::stable_vector
      <MyInt, Allocator> type;
   static const char *vector_name()
   {
      return "stable_vector<MyInt>";
   }
};

template<template<class> class GetContainer, class Allocator>
void stable_vector_test_template(std::size_t num_iterations, std::size_t num_elements, bool csv_output)
{
   typedef typename GetContainer<Allocator>::type vector_type;
   //std::size_t top_capacity = 0;
   nanosecond_type nseconds;
   {
      {
         vector_type l;
         cpu_timer timer;
         timer.resume();

         for(std::size_t r = 0; r != num_iterations; ++r){
            l.insert(l.end(), num_elements, MyInt((int)r));
         }

         timer.stop();
         nseconds = timer.elapsed().wall;

         if(csv_output){
            std::cout   << get_allocator_name<Allocator>::get()
                        << ";"
                        << GetContainer<Allocator>::vector_name()
                        << ";"
                        << num_iterations
                        << ";"
                        << num_elements
                        << ";"
                        << float(nseconds)/float(num_iterations*num_elements)
                        << ";";
         }
         else{
            std::cout   << "Allocator: " << get_allocator_name<Allocator>::get()
                        << '\t'
                        << GetContainer<Allocator>::vector_name()
                        << std::endl
                        << "  allocation ns:   "
                        << float(nseconds)/float(num_iterations*num_elements);
         }
//         top_capacity = l.capacity();
         //Now preprocess ranges to erase
         std::vector<typename vector_type::iterator> ranges_to_erase;
         ranges_to_erase.push_back(l.begin());
         for(std::size_t r = 0; r != num_iterations; ++r){
            typename vector_type::iterator next_pos(ranges_to_erase[r]);
            std::size_t n = num_elements;
            while(n--){ ++next_pos; }
            ranges_to_erase.push_back(next_pos);
         }

         //Measure range erasure function
         timer.stop();
         timer.start();

         for(std::size_t r = 0; r != num_iterations; ++r){
            std::size_t init_pos = (num_iterations-1)-r;
            l.erase(ranges_to_erase[init_pos], l.end());
         }
         timer.stop();
         nseconds = timer.elapsed().wall;
         assert(l.empty());
      }
   }

   if(csv_output){
      std::cout      << float(nseconds)/float(num_iterations*num_elements)
                     << std::endl;
   }
   else{
      std::cout      << '\t'
                     << "  deallocation ns: "
                     << float(nseconds)/float(num_iterations*num_elements)/*
                     << std::endl
                     << "  max capacity:    "
                     << static_cast<std::size_t>(top_capacity)
                     << std::endl
                     << "  remaining cap.   "
                     << static_cast<std::size_t>(top_capacity - num_iterations*num_elements)
                     << " (" << (float(top_capacity)/float(num_iterations*num_elements) - 1)*100 << " %)"*/
                     << std::endl << std::endl;
   }
   assert(bc::dlmalloc_all_deallocated());
   bc::dlmalloc_trim(0);
}

void print_header()
{
   std::cout   << "Allocator" << ";" << "Iterations" << ";" << "Size" << ";"
               << "Insertion time(ns)" << ";" << "Erasure time(ns)" << ";"
               << std::endl;
}

void stable_vector_operations()
{
   {
      bc::stable_vector<int> a(bc::stable_vector<int>::size_type(5), 4);
      bc::stable_vector<int> b(a);
      bc::stable_vector<int> c(a.cbegin(), a.cend());
      b.insert(b.cend(), 0);
      c.pop_back();
      a.assign(b.cbegin(), b.cend());
      a.assign(c.cbegin(), c.cend());
      a.assign(1, 2);
   }
   {
      typedef bc::stable_vector<int, std::allocator<int> > stable_vector_t;
      stable_vector_t a(bc::stable_vector<int>::size_type(5), 4);
      stable_vector_t b(a);
      stable_vector_t c(a.cbegin(), a.cend());
      b.insert(b.cend(), 0);
      c.pop_back();
      assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
      a.assign(b.cbegin(), b.cend());
      assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
      a.assign(c.cbegin(), c.cend());
      assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
      a.assign(1, 2);
      assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
      a.reserve(100);
      assert(static_cast<std::size_t>(a.end() - a.begin()) == a.size());
   }
}

int main(int argc, const char *argv[])
{
   //#define SINGLE_TEST
   #define SIMPLE_IT
   #ifdef SINGLE_TEST
      #ifdef NDEBUG
      std::size_t numit [] = { 40 };
      #else
      std::size_t numit [] = { 4 };
      #endif
      std::size_t numele [] = { 10000 };
   #elif defined(SIMPLE_IT)
      std::size_t numit [] = { 3 };
      std::size_t numele [] = { 10000 };
   #else
      #ifdef NDEBUG
      std::size_t numit [] = { 40, 400, 4000, 40000 };
      #else
      std::size_t numit [] = { 4,   40,   400,   4000 };
      #endif
      std::size_t numele [] = { 10000, 1000, 100,   10     };
   #endif

   //Warning: range erasure is buggy. Vector iterators are not stable, so it is not
   //possible to cache iterators, but indexes!!!

   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){
         stable_vector_test_template<get_stable_vector, StdAllocator>(numit[i], numele[i], csv_output);
      }
      for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
         stable_vector_test_template<get_vector, StdAllocator>(numit[i], numele[i], csv_output);
      }
      for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
         stable_vector_test_template<get_stable_vector, AllocatorPlusV1>(numit[i], numele[i], csv_output);
      }
      for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
         stable_vector_test_template<get_vector, AllocatorPlusV1>(numit[i], numele[i], csv_output);
      }
      for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
         stable_vector_test_template<get_stable_vector, AllocatorPlusV2>(numit[i], numele[i], csv_output);
      }
      for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
         stable_vector_test_template<get_vector, AllocatorPlusV2>(numit[i], numele[i], csv_output);
      }
      for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
         stable_vector_test_template<get_stable_vector, AdPool2PercentV2>(numit[i], numele[i], csv_output);
      }
      for(std::size_t i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
         stable_vector_test_template<get_vector, AdPool2PercentV2>(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";
         stable_vector_test_template<get_stable_vector, StdAllocator>(numit[i], numele[i], csv_output);
         stable_vector_test_template<get_vector, StdAllocator>(numit[i], numele[i], csv_output);
         stable_vector_test_template<get_stable_vector, AllocatorPlusV1>(numit[i], numele[i], csv_output);
         stable_vector_test_template<get_vector, AllocatorPlusV1>(numit[i], numele[i], csv_output);
         stable_vector_test_template<get_stable_vector, AllocatorPlusV2>(numit[i], numele[i], csv_output);
         stable_vector_test_template<get_vector, AllocatorPlusV2>(numit[i], numele[i], csv_output);
         stable_vector_test_template<get_stable_vector, AdPool2PercentV2>(numit[i], numele[i], csv_output);
         stable_vector_test_template<get_vector, AdPool2PercentV2>(numit[i], numele[i], csv_output);
      }
   }

   return 0;
}