stdio performance in tango, stdlib, and perl

James Dennett jdennett at acm.org
Thu Mar 22 23:23:35 PDT 2007


Andrei Alexandrescu (See Website For Email) wrote:
> James Dennett wrote:
>> 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.)
> 
> With your code pasted and wind from behind:
> 
> 13.5s        cppcat

Blasted weather.  Never a hurricane when you need one.

>> 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++.
> 
> The sync_with_stdio and tie tricks are already unknown to most
> programmers, so it would be an uphill battle to characterize them as
> idiomatic. They are idiomatic for a small group at best.

IOStreams is a terrible chunk of library design, and
its effective use is fiendishly difficult even for
fairly trivial tasks.  I've implemented large chunks
of the C++ standard library, but IOStreams scares me.

> But, obviously not enough. Perl does way better.
> 
> (Again: gcc on Linux.)

Most of the time I do large text processing jobs in
Perl or inside a database; once in a while I use C++,
primarily if I need to do trickier calculations.
No good reason D shouldn't be able to handle the
jobs I use C++ for in this area (though I'd have
to get D working on Solaris, and 64-bit support
would probably be necessary).

-- James



More information about the Digitalmars-d mailing list