M
Mark
I am using gnu g++ version 3.3.2,
trying a simple test to read in and then
write out a large (100,000 line) text file
##########################################
CSTDIO VERSION TO READ/WRITE TEXT FILE:
#include <cstdlib>
#include <cstdio>
using namespace std;
main(int argc, void** argv) {
FILE* in;
if ((in = fopen("zinput","r")) == NULL) { exit(1); }
FILE* out;
if ((out = fopen("zoutput","w")) == NULL) { exit(1); }
char string[200];
while (fgets(string,199,in) != NULL) {
fprintf(out, "%s", string);
}
}
##########################################
EQUIVALENT IOSTREAM VERSION TO READ/WRITE TEXT FILE:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
main(int argc, void** argv) {
ifstream input("zinput");;
if ( !input.good() ) { exit(-1); }
ofstream output("zoutput");
if ( !output.good() ) { exit(-1); }
char string[200];
while ( !input.getline(string, 199).eof() ) {
output << string << endl;
}
}
##########################################
The problem is, the CSTDIO version runs
10 TIMES FASTER thant the IOSTREAM version!
Here are results from running the program with
the time command:
CSTDIO VERSION:
real 0.2
user 0.1
sys 0.0
IOSTREAM VERSION:
real 2.6
user 1.0
sys 1.4
Does anyone know why the IOSTREAM version of this
simple program is so much slower than the CSTDIO version?
trying a simple test to read in and then
write out a large (100,000 line) text file
##########################################
CSTDIO VERSION TO READ/WRITE TEXT FILE:
#include <cstdlib>
#include <cstdio>
using namespace std;
main(int argc, void** argv) {
FILE* in;
if ((in = fopen("zinput","r")) == NULL) { exit(1); }
FILE* out;
if ((out = fopen("zoutput","w")) == NULL) { exit(1); }
char string[200];
while (fgets(string,199,in) != NULL) {
fprintf(out, "%s", string);
}
}
##########################################
EQUIVALENT IOSTREAM VERSION TO READ/WRITE TEXT FILE:
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
main(int argc, void** argv) {
ifstream input("zinput");;
if ( !input.good() ) { exit(-1); }
ofstream output("zoutput");
if ( !output.good() ) { exit(-1); }
char string[200];
while ( !input.getline(string, 199).eof() ) {
output << string << endl;
}
}
##########################################
The problem is, the CSTDIO version runs
10 TIMES FASTER thant the IOSTREAM version!
Here are results from running the program with
the time command:
CSTDIO VERSION:
real 0.2
user 0.1
sys 0.0
IOSTREAM VERSION:
real 2.6
user 1.0
sys 1.4
Does anyone know why the IOSTREAM version of this
simple program is so much slower than the CSTDIO version?