stdio performance in tango, stdlib, and perl

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Thu Mar 22 09:14:14 PDT 2007


Vladimir Panteleev wrote:
> On Thu, 22 Mar 2007 01:40:15 +0200, Andrei Alexandrescu (See Website For Email) <SeeWebsiteForEmail at erdani.org> wrote:
> 
>> Essentially it's about information. The naive loop:
>>
>> while (readln(line)) {
>>    write(line);
>> }
>>
>> is guaranteed 100% to produce an accurate copy of its input. The version
>> that chops lines looks like:
>>
>> while (readln(line)) {
>>    writeln(line);
>> }
> 
> I'd just like to say that the chosen naming convention seems a bit unintuitive to me out of the following reasons:
> 
> 1) it seems odd that what you read with readln(), you need to write with write() and not writeln().

"Read a line. Write what you've read. Rinse. Lather. Repeat."

> 2) Pascal/Delphi/etc. have the ReadLn and WriteLn functions, but Pascal's ReadLn doesn't preserve line endings.

That's a mistake, simple as that. Pascal has made many other similar 
mistakes, see http://www.lysator.liu.se/c/bwk-on-pascal.html.

> 3) in my personal experience (of a number of smaller and larger console applications), it's much more often that I need to work with the contents of lines (without line endings), rather than with. If you need to copy data while preserving line endings, I would recommend using binary buffers for files - and I've no idea why would you use standard input/output for binary data anyway.

I understand that. But again, getting rid of information when you have 
it is a much better proposition than regaining information when you 
irremediably lost.

Think that a file is produced by a utility or transmission that sends 
messages separated by a single-char or multi-char separator. If your 
reading primitive omits the separator, you don't know whether the last 
line is a fragment of a broken transmission or a valid line.

"Just call chomp."

> 4) it's much easier to add a line ending than to remove it.

It's been already said: it's cheaper to remove it in all circumstances.

> Based on the above reasons, I would like to suggest to let readln() chop line endings, and perhaps have another function (getline?) which keeps them.

For more balkanization, cognitive load, and confusion?

"Just call chomp."


Andrei



More information about the Digitalmars-d mailing list