| 1 | // Copyright (C) 2003-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 | #include <ctime> |
| 20 | #include <iostream> |
| 21 | #include <string> |
| 22 | #include <testsuite_performance.h> |
| 23 | |
| 24 | using namespace std; |
| 25 | |
| 26 | void |
| 27 | test_append_char(int how_much) |
| 28 | { |
| 29 | string buf; // no preallocation |
| 30 | for (int i = 0; i < how_much; ++i) |
| 31 | buf.append(static_cast<string::size_type>(1) , 'x'); |
| 32 | } |
| 33 | |
| 34 | void |
| 35 | test_append_string(int how_much) |
| 36 | { |
| 37 | string s(static_cast<string::size_type>(1) , 'x'); |
| 38 | string buf; // no preallocation |
| 39 | for (int i = 0; i < how_much; ++i) |
| 40 | buf.append(s); |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | run_benchmark1(int how_much) |
| 45 | { |
| 46 | using namespace __gnu_test; |
| 47 | time_counter time; |
| 48 | resource_counter resource; |
| 49 | start_counters(time, resource); |
| 50 | test_append_char(how_much); |
| 51 | stop_counters(time, resource); |
| 52 | report_performance(__FILE__, "char", time, resource); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | run_benchmark2(int how_much) |
| 57 | { |
| 58 | using namespace __gnu_test; |
| 59 | time_counter time; |
| 60 | resource_counter resource; |
| 61 | start_counters(time, resource); |
| 62 | test_append_string(how_much); |
| 63 | stop_counters(time, resource); |
| 64 | report_performance(__FILE__, "string", time, resource); |
| 65 | } |
| 66 | |
| 67 | // libstdc++/5380 |
| 68 | // libstdc++/4960 |
| 69 | int main() |
| 70 | { |
| 71 | run_benchmark1(100000); |
| 72 | run_benchmark2(100000); |
| 73 | run_benchmark1(1000000); |
| 74 | run_benchmark2(1000000); |
| 75 | run_benchmark1(10000000); |
| 76 | run_benchmark2(10000000); |
| 77 | } |