www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - When to prefer formatValue over formattedWrite?

reply Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
Is `formatValue` also preferred over `formattedWrite` when 
writing a single value?


Code example:

import std.array : appender;

auto writer1 = appender!string();
writer1.formattedWrite("%08b", 42);

auto writer2 = appender!string();
auto f = singleSpec("%08b");
writer2.formatValue(42, f);


Is `formatValue` more lightweight to the compiler?
Feb 19
parent Per =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
On Tuesday, 19 February 2019 at 11:40:09 UTC, Per Nordlöw wrote:
 Is `formatValue` more lightweight to the compiler?
I already posted a very similar question at https://forum.dlang.org/post/xsovipkjyjyumtyzmwqi forum.dlang.org but in this thread I'm interested in knowing if is there is any significant difference in compilation-speed and if formatValue and/or formattedWrite needs to allocate using the GC when we are writing a array of `char`s or a long, directly to standard output. In theory the string could be written directly to stdout without an extra memory buffer and the `long` could be formatted to a temporarily (stack) allocated array of `char`s before written to standard output.
Feb 19