question when writing to a file

bioinfornatics bioinfornatics at fedoraproject.org
Thu Mar 7 08:20:23 PST 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)


More information about the Digitalmars-d-learn mailing list