stdio performance in tango, stdlib, and perl
James Dennett
jdennett at acm.org
Wed Mar 21 20:53:33 PDT 2007
Andrei Alexandrescu (See Website For Email) wrote:
> Walter Bright wrote:
>> Andrei Alexandrescu (See Website For Email) wrote:
>>> I've ran a couple of simple tests comparing Perl, D's stdlib (the
>>> coming release), and Tango.
>>
>> Can you add a C++ <iostream> to the mix? I think that would be a very
>> useful additional data point.
>
> Obliged. Darn, I had to wait a *lot* longer.
>
> #include <string>
> #include <iostream>
>
> int main() {
> std::string s;
> while (getline(std::cin, s)) {
> std::cout << s << '\n';
> }
> }
>
> (C++ makes the same mistake wrt newline.)
>
> 35.7s cppcat
>
> I seem to remember a trick that puts some more wind into iostream's
> sails, so I tried that as well:
>
> #include <string>
> #include <iostream>
> using namespace std;
>
> int main() {
> cin.sync_with_stdio(false);
> cout.sync_with_stdio(false);
> string s;
> while (getline(std::cin, s)) {
> cout << s << '\n';
> }
> }
>
> Result:
>
> 13.3s cppcat
Try the way IOStreams would be used if you didn't want
it to go slowly:
#include <string>
#include <iostream>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::string s;
while (std::getline(std::cin, s)) {
std::cout << s << '\n';
}
}
(Excuse the lack of a using directive there; I find the
code more readable without them. YMMV.)
I don't have your sample file or your machine, but for
the quick tests I just ran on this one machine, the code
above runs move than 60% faster. Without using tie(),
each read from standard input causes a flush of standard
output (so that, by default, they work appropriately for
console I/O).
It's certainly true that making efficient use of IOStreams
needs some specific knowledge, and that writing an
efficient implementation of IOStreams is far from trivial.
But if we're comparing to C++, we should probably compare
to some reasonably efficient idiomatic C++.
-- James
More information about the Digitalmars-d
mailing list