Hi,
I wrote the attached simple benchmark program to compare iostreams
and C stdio. Running it with -10000000 and 10000000 on the command
line on Linux 2.6.9 (GNU libc 2.5) I get the numbers below for each
of the implementations I tried when using /dev/null and /tmp/file
as the sink:
/dev/null /tmp/file
CLK:USR:SYS CLK:USR:SYS
stdio 28s:27s:0.0s 32s:28s:1.8s
g++ 4.1.1 42s:42s:0.0s 44s:42s:2.0s
stdcxx trunk 40s:41s:0.0s 49s:41s:5.8s
STLport 5.1.3 27s:27s:0.0s 30s:27s:2.3s
The CLK, USR, and SYS abbreviations stand for wall clock, user time,
and system time. In all cases I used -D_REENTRANT, -O2 and -pthread
to compile and link.
-- Mark
#include <cstdio>
#include <cstdlib>
#include <ios>
#include <iostream>
#include <fstream>
const char* strings[] = {
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen", "twenty"
};
int test (int from, int to, const char *fname, bool stdio = false)
{
if (stdio) {
std::cout << "Running stdio loop: " << from << ", " << to << '\n';
std::FILE* f = std::fopen (fname, "w");
if (!f)
return 1;
for (int i = from; i < to; ++i) {
short s = i;
const char* str =
strings [i % (sizeof strings / sizeof *strings)];
if (0 > std::fprintf (f, "\"%s\" %hd %x\n", str, s, i)) {
std::cerr << "Error writing " << fname << " at " << i << '\n';
return 1;
}
}
return 0;
}
std::cout << "Running iostream loop: " << from << ", " << to << '\n';
std::ofstream strm (fname);
for (int i = from; i < to; ++i) {
short s = i;
const char* str =
strings [i % (sizeof strings / sizeof *strings)];
if (!(strm << '"' << str << "\" "
<< s << ' '
<< i << ' '
<< '\n')) {
std::cerr << "Error writing " << fname << " at " << i << '\n';
return 1;
}
}
return 0;
}
int main (int argc, char *argv [])
{
int from = 1 < argc ? std::atoi (argv [1]) : 0;
int to = 2 < argc ? std::atoi (argv [2]) : 0;
const char* fname = 3 < argc ? argv [3] : "/dev/null";
bool stdio = 4 < argc;
test (from, to, fname, stdio);
}