Tips for fast string concatenation?

Jonathan M Davis jmdavisProg at gmx.com
Fri Jun 21 03:14:38 PDT 2013


On Friday, June 21, 2013 12:09:09 Gary Willoughby wrote:
> Have you any tips for using D when you need fast string
> concatenation? I regularly use code like this:
> 
>      foreach (i, range)
>      {
>          foo ~= bar;
>      }
> 
> or:
> 
>      foo = foo ~ bar ~ baz ~ qux;
> 
> I've used std.string.format(...) in some instances which sped
> things up which surprised me.
> 
> Are there faster ways of appending strings?

In general, ~= will be faster, beacause it won't create temporaries like 
concatenating a bunch of strings in a single expression would. However, if you 
want faster appending, generally the thing to use is std.array.Appender. And 
if you want to use format strings, it can be used with 
std.format.formattedWrite.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list