stdio performance in tango, stdlib, and perl

Daniel Keep daniel.keep.lists at gmail.com
Thu Mar 22 02:03:12 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().

I suppose it is a little, but I think that's more an issue with text IO
in general; for instance, even *if* readln discarded the line ending,
readln and writeln wouldn't be symmetric anyway!  If you expect them to
be, then you're in for a nasty surprise :P

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

Well, that's Pascal/Delphi/etc., not D.

> 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.

That's a valid point; I rarely need the line endings, that said, see [1] :)

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

Actually, it's not.  Removing a line ending is as simple as slicing the
string.  *Adding* a line ending could involve a heap allocation, at
least a full copy.

What's more, how can you be sure there was a line-ending there at all?
What if it's the last line and it didn't have a line ending before EOF?

> 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.

[1]

There have been a few times I've needed the line-ending, and it's a
major pain when your IO library simply refuses to give it to you.  It
should be that the call gives you the whole line *including*
line-endings, but since stripping the line of its ending is so common
there should be either another function to do that, or a nice shortcut
to get it done.

Maybe we need readln and readlt for "read line and trim"...

</2c>

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list