| 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 |
#include <unistd.h> |
| 19 |
#include <cstdio> |
| 20 |
#include <fstream> |
| 21 |
#include <string> |
| 22 |
#include <testsuite_performance.h> |
| 23 |
|
| 24 |
// libstdc++/15002 |
| 25 |
int main() |
| 26 |
{ |
| 27 |
using namespace std; |
| 28 |
using namespace __gnu_test; |
| 29 |
|
| 30 |
time_counter time; |
| 31 |
resource_counter resource; |
| 32 |
|
| 33 |
const char filename[] = "tmp_getline.txt"; |
| 34 |
const unsigned lines = 1000000; |
| 35 |
const unsigned line_length = 200; |
| 36 |
|
| 37 |
char* line = new char[line_length + 2]; |
| 38 |
|
| 39 |
// Construct data. |
| 40 |
{ |
| 41 |
memset(line, 'x', line_length); |
| 42 |
line[line_length] = '\n'; |
| 43 |
line[line_length + 1] = '\0'; |
| 44 |
|
| 45 |
ofstream out(filename); |
| 46 |
for (unsigned i = 0; i < lines; ++i) |
| 47 |
out << line; |
| 48 |
} |
| 49 |
|
| 50 |
// C |
| 51 |
{ |
| 52 |
// Fill the cache. |
| 53 |
FILE *file = fopen(filename, "r"); |
| 54 |
while (fgets(line, line_length + 2, file)); |
| 55 |
fclose(file); |
| 56 |
|
| 57 |
file = fopen(filename, "r"); |
| 58 |
start_counters(time, resource); |
| 59 |
while (fgets(line, line_length + 2, file)); |
| 60 |
stop_counters(time, resource); |
| 61 |
fclose(file); |
| 62 |
report_performance(__FILE__, "C (fgets)", time, resource); |
| 63 |
clear_counters(time, resource); |
| 64 |
} |
| 65 |
|
| 66 |
// getline(basic_istream<_CharT, _Traits>& __in, |
| 67 |
// basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim) |
| 68 |
{ |
| 69 |
ifstream file(filename); |
| 70 |
string string_line; |
| 71 |
|
| 72 |
start_counters(time, resource); |
| 73 |
while (getline(file, string_line)); |
| 74 |
stop_counters(time, resource); |
| 75 |
report_performance(__FILE__, "C++ (string)", time, resource); |
| 76 |
clear_counters(time, resource); |
| 77 |
} |
| 78 |
|
| 79 |
// getline(char_type* __s, streamsize __n, char_type __delim) |
| 80 |
{ |
| 81 |
ifstream file(filename); |
| 82 |
|
| 83 |
start_counters(time, resource); |
| 84 |
while (file.getline(line, line_length + 2)); |
| 85 |
stop_counters(time, resource); |
| 86 |
report_performance(__FILE__, "C++ (char array)", time, resource); |
| 87 |
clear_counters(time, resource); |
| 88 |
} |
| 89 |
|
| 90 |
delete[] line; |
| 91 |
unlink(filename); |
| 92 |
return 0; |
| 93 |
} |