16728.cc 2.8 KB

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