1// Copyright (C) 2004-2015 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 <cstdio> 20#include <cstdlib> 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]; 37 const char* name1 = "/usr/share/dict/words"; 38 const char* name2 = "/usr/share/dict/linux.words"; 39 const char* name = name1; 40 41 // C 42 FILE* file; 43 if (!(file = fopen(name, "r"))) 44 { 45 name = name2; 46 if (!(file = fopen(name, "r"))) 47 exit(1); 48 } 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} 86