digitalmars.D.learn - question when writing to a file
- bioinfornatics (9/9) Mar 07 2013 Dear,
- bearophile (15/16) Mar 07 2013 To be sure of what's faster you often have to write small
- lomereiter (15/24) Mar 07 2013 The second is probably faster (with optimizations enabled),
- bioinfornatics (3/35) Mar 07 2013 and if is not stdout but File ?
- bioinfornatics (7/47) Mar 07 2013 I only replace write by put and do this:
- lomereiter (5/11) Mar 07 2013 Indeed, that shouldn't be the case. I filed a bug request:
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
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
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
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:and if is not stdout but File ? thanks a lot that is really interestingDear, 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
On Thursday, 7 March 2013 at 16:12:09 UTC, bioinfornatics wrote:On Thursday, 7 March 2013 at 13:37:56 UTC, lomereiter 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. /std/stdio.d(1267): (Bad file descriptor)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:and if is not stdout but File ? thanks a lot that is really interestingDear, 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
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