| 1 |
// Copyright (C) 2004-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
|
| 19 |
/* |
| 20 |
* The goal with this application is to compare the performance |
| 21 |
* between different std::allocator implementations. The results are |
| 22 |
* influenced by the underlying allocator in the "C" library, malloc. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <set> |
| 26 |
#include <sstream> |
| 27 |
|
| 28 |
using namespace std; |
| 29 |
|
| 30 |
typedef int test_type; |
| 31 |
|
| 32 |
// This can take extremely long on simulators, timing out the test. |
| 33 |
// { dg-options "-DITERATIONS=10" { target simulator } } |
| 34 |
#ifndef ITERATIONS |
| 35 |
#define ITERATIONS 10000 |
| 36 |
#endif |
| 37 |
|
| 38 |
// The number of iterations to be performed. |
| 39 |
int iterations = ITERATIONS; |
| 40 |
|
| 41 |
// The number of values to insert in the container, 32 will cause 5 |
| 42 |
// (re)allocations to be performed (sizes 4, 8, 16, 32 and 64) |
| 43 |
// This means that all allocations are within _MAX_BYTES = 128 as |
| 44 |
// defined in stl_alloc.h for __pool_alloc. Whether or not this |
| 45 |
// value is relevant in "the real world" or not I don't know and |
| 46 |
// should probably be investigated in more detail. |
| 47 |
int insert_values = 128; |
| 48 |
|
| 49 |
template<typename TestType> |
| 50 |
struct value_type : public pair<TestType, TestType> |
| 51 |
{ |
| 52 |
value_type() : pair<TestType, TestType>(0, 0) { } |
| 53 |
|
| 54 |
inline value_type operator++() { return ++this->first, *this; } |
| 55 |
inline operator TestType() const { return this->first; } |
| 56 |
}; |
| 57 |
|
| 58 |
template<typename Container> |
| 59 |
void |
| 60 |
do_loop() |
| 61 |
{ |
| 62 |
Container obj; |
| 63 |
int test_iterations = 0; |
| 64 |
value_type<test_type> test_value; |
| 65 |
while (test_iterations < iterations) |
| 66 |
{ |
| 67 |
for (int j = 0; j < insert_values; ++j) |
| 68 |
obj.insert(obj.end(), ++test_value); |
| 69 |
++test_iterations; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
template<typename Container> |
| 74 |
void |
| 75 |
test_container(Container, bool run_threaded = false) |
| 76 |
{ |
| 77 |
do_loop<Container>(); |
| 78 |
std::ostringstream comment; |
| 79 |
if (run_threaded) |
| 80 |
comment << "4-way threaded iterations: " << iterations*4 << '\t'; |
| 81 |
else |
| 82 |
comment << "iterations: " << iterations << '\t'; |
| 83 |
} |
| 84 |
|
| 85 |
// http://gcc.gnu.org/ml/libstdc++/2001-05/msg00105.html |
| 86 |
// http://gcc.gnu.org/ml/libstdc++/2003-05/msg00231.html |
| 87 |
int main(void) |
| 88 |
{ |
| 89 |
typedef less<test_type> compare_type; |
| 90 |
test_container(set<test_type, compare_type>()); |
| 91 |
return 0; |
| 92 |
} |