Appender at CTFE?

cym13 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 21 16:51:15 PDT 2015


On Friday, 21 August 2015 at 22:39:29 UTC, Nick Sabalausky wrote:
> Not at a pc, so can't test right now, but does Appender work at 
> compile time? If not, does ~= still blow up CTFE memory usage 
> like it used to? Any other best practice / trick for building 
> strings in CTFE?

I did two experiments:

     import std.conv;
     import std.stdio;
     import std.array;
     import std.range;
     import std.algorithm;

     string f(T)(T strings) {
         auto a = appender("");
         foreach (s ; strings)
             a.put(s);
         return a.data;
     }

     string g(T)(T strings) {
         auto a = "";
         foreach (s ; strings)
             a ~= s;
         return a;
     }

     void main(string[] args) {
         enum a = iota(10000).map!(to!string).f;
       //enum a = iota(10000).map!(to!string).g;
         a.writeln;
     }

Each make use of CTFE but the f (appender) variant blew my RAM 
(old computer)
while the g (string) variant was nothing comparable in term of 
memory
consumption and almost instantaneous. It looks like string 
concatenation is
the best way to go, but this example is rather limited so 
theoretical
confirmation would be great.



More information about the Digitalmars-d-learn mailing list