www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - question when writing to a file

reply "bioinfornatics" <bioinfornatics fedoraproject.org> writes:
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?
Mar 07 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Mar 07 2013
prev sibling parent reply "lomereiter" <lomereiter gmail.com> writes:
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?
Mar 07 2013
parent reply "bioinfornatics" <bioinfornatics fedoraproject.org> writes:
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
Mar 07 2013
parent reply "bioinfornatics" <bioinfornatics fedoraproject.org> writes:
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. /std/stdio.d(1267): (Bad file descriptor)
Mar 07 2013
parent "lomereiter" <lomereiter gmail.com> writes:
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)
Mar 07 2013