]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/performance/27_io/filebuf_sgetn_unbuf.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / performance / 27_io / filebuf_sgetn_unbuf.cc
CommitLineData
8d9254fc 1// Copyright (C) 2004-2020 Free Software Foundation, Inc.
c56e3d82
PC
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
748086b7 6// Free Software Foundation; either version 3, or (at your option)
c56e3d82
PC
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
748086b7
JJ
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
c56e3d82
PC
18
19#include <cstdio>
debac9f4 20#include <cstdlib>
c56e3d82
PC
21#include <fstream>
22#include <testsuite_performance.h>
23
24// libstdc++/11722
25int main()
26{
27 using namespace std;
28 using namespace __gnu_test;
29
30 time_counter time;
31 resource_counter resource;
32
33 const int iterations = 500000;
34 const int chunksize = 100;
35
36 char* chunk = new char[chunksize];
a780ad2f
PC
37 const char* name1 = "/usr/share/dict/words";
38 const char* name2 = "/usr/share/dict/linux.words";
39 const char* name = name1;
c56e3d82
PC
40
41 // C
a780ad2f
PC
42 FILE* file;
43 if (!(file = fopen(name, "r")))
44 {
45 name = name2;
46 if (!(file = fopen(name, "r")))
47 exit(1);
48 }
c56e3d82
PC
49 setvbuf(file, 0, _IONBF, 0);
50 start_counters(time, resource);
51 for (int i = 0; i < iterations; ++i)
52 if (fread(chunk, 1, chunksize, file) < chunksize)
53 fseek(file, 0, SEEK_SET);
54 stop_counters(time, resource);
55 fclose(file);
56 report_performance(__FILE__, "C", time, resource);
57 clear_counters(time, resource);
58
59 // C unlocked
60 file = fopen(name, "r");
61 setvbuf(file, 0, _IONBF, 0);
62 start_counters(time, resource);
63 for (int i = 0; i < iterations; ++i)
64 if (fread_unlocked(chunk, 1, chunksize, file) < chunksize)
65 fseek(file, 0, SEEK_SET);
66 stop_counters(time, resource);
67 fclose(file);
68 report_performance(__FILE__, "C unlocked", time, resource);
69 clear_counters(time, resource);
70
71 // C++
72 filebuf buf;
73 buf.pubsetbuf(0, 0);
74 buf.open(name, ios_base::in);
75 start_counters(time, resource);
76 for (int i = 0; i < iterations; ++i)
77 if (buf.sgetn(chunk, chunksize) < chunksize)
78 buf.pubseekoff(0, ios::beg);
79 stop_counters(time, resource);
80 report_performance(__FILE__, "C++", time, resource);
81
82 delete [] chunk;
83
84 return 0;
85}