2018-03-08 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libstdc++-v3 / testsuite / performance / 27_io / ifstream_getline-2.cc
blobe084aabce8fe25be1c5e1a354cc5ca9eac0950f2
1 // Copyright (C) 2004-2018 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.
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.
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/>.
18 #include <unistd.h>
19 #include <cstdio>
20 #include <fstream>
21 #include <string>
22 #include <testsuite_performance.h>
24 // libstdc++/15002
25 int main()
27 using namespace std;
28 using namespace __gnu_test;
30 time_counter time;
31 resource_counter resource;
33 const char filename[] = "tmp_getline.txt";
34 const unsigned lines = 1000000;
35 const unsigned line_length = 200;
37 char* line = new char[line_length + 2];
39 // Construct data.
41 memset(line, 'x', line_length);
42 line[line_length] = '\n';
43 line[line_length + 1] = '\0';
45 ofstream out(filename);
46 for (unsigned i = 0; i < lines; ++i)
47 out << line;
50 // C
52 // Fill the cache.
53 FILE *file = fopen(filename, "r");
54 while (fgets(line, line_length + 2, file));
55 fclose(file);
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);
66 // getline(basic_istream<_CharT, _Traits>& __in,
67 // basic_string<_CharT, _Traits, _Alloc>& __str, _CharT __delim)
69 ifstream file(filename);
70 string string_line;
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);
79 // getline(char_type* __s, streamsize __n, char_type __delim)
81 ifstream file(filename);
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);
90 delete[] line;
91 unlink(filename);
92 return 0;