question when writing to a file

lomereiter lomereiter at gmail.com
Thu Mar 7 05:37:55 PST 2013


The second is probably faster (with optimizations enabled), 
because each call to writeln incurs overhead of locking/unlocking 
the file stream (which is stdout in this case).

If you need to print huge amounts of data, use lockingTextWriter 
like this:

auto w = stdout.lockingTextWriter;
foreach (i; 0 .. 5) {
     w.put("sometext\n");
     w.formattedwrite("%d\n", some_number);
}

However, std.format.formattedWrite is also relatively slow, so 
it's better to prepare string representation of an object in a 
buffer on the stack - say, with snprintf, or your own set of 
formatting functions - and then put it into the writer.

On Thursday, 7 March 2013 at 10:04:34 UTC, bioinfornatics wrote:
> Dear,
>
> little question when writing to a file 5 "hello" lines (by 
> example)
> I would like to know if they are a difference between:
>
> writeln( "hello" ); x5
>
> and:
>
> string[] helloList = [ "hello","hello","hello","hello","hello"];
> writeln( helloList.join( newline) );
>
>
> I mean if one way is more efficient by speed?



More information about the Digitalmars-d-learn mailing list