Thread overview
question when writing to a file
Mar 07, 2013
bioinfornatics
Mar 07, 2013
bearophile
Mar 07, 2013
lomereiter
Mar 07, 2013
bioinfornatics
Mar 07, 2013
bioinfornatics
Mar 07, 2013
lomereiter
March 07, 2013
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?
March 07, 2013
bioinfornatics:

> I mean if one way is more efficient by speed?

To be sure of what's faster you often have to write small benchmarks.

In this case this should be good:

import std.stdio, std.array;
void main() {
    writefln("%-(%s\n%)", ["hello"].replicate(5));
}


But if you want speed this is is probably faster:

import core.stdc.stdio: puts;
void main() {
    puts("hello\nhello\nhello\nhello\nhello");
}

Bye,
bearophile
March 07, 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?

March 07, 2013
On Thursday, 7 March 2013 at 13:37:56 UTC, lomereiter wrote:
> 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?

and if is not stdout but File ?

thanks a lot that is really interesting
March 07, 2013
On Thursday, 7 March 2013 at 16:12:09 UTC, bioinfornatics wrote:
> On Thursday, 7 March 2013 at 13:37:56 UTC, lomereiter wrote:
>> 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?
>
> and if is not stdout but File ?
>
> thanks a lot that is really interesting

I only replace write by put and do this:
auto output = File( f.absolutePath().expandTilde(), "w" ).lockingTextWriter();


that build without get an error but at runtime i got:
std.exception.ErrnoException@/env/export/nfs2/cns_prog/opt/gdc/include/d/4.7.2/std/stdio.d(1267):  (Bad file descriptor)
March 07, 2013
Indeed, that shouldn't be the case. I filed a bug request:

http://d.puremagic.com/issues/show_bug.cgi?id=9661

While it isn't fixed, assign file to a variable so that it doesn't go out of scope.

On Thursday, 7 March 2013 at 16:20:24 UTC, bioinfornatics wrote:
>
> I only replace write by put and do this:
> auto output = File( f.absolutePath().expandTilde(), "w" ).lockingTextWriter();
>
>
> that build without get an error but at runtime i got:
> std.exception.ErrnoException@/env/export/nfs2/cns_prog/opt/gdc/include/d/4.7.2/std/stdio.d(1267):
>  (Bad file descriptor)