www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Tips for fast string concatenation?

reply "Gary Willoughby" <dev kalekold.net> writes:
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?
Jun 21 2013
next sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Friday, 21 June 2013 at 10:09:10 UTC, Gary Willoughby wrote:
 Are there faster ways of appending strings?
You'll want to use appender, from std.array: http://dlang.org/phobos/std_array.html#.Appender
Jun 21 2013
prev sibling next sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
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
Jun 21 2013
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 21 Jun 2013 06:14:38 -0400, Jonathan M Davis <jmdavisProg gmx.com>  
wrote:

 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.
I believe the above is one call to the runtime. To answer the OP, using reserve will speed up the allocation quite a bit. Appender is certainly the fastest method. -Steve
Jun 21 2013
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Friday, 21 June 2013 at 10:09:10 UTC, 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?
Regardless of whether you end up using lots of ~=, or Appender (possibly with formattedWrite), using reserve never hurts.
Jun 21 2013
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Friday, 21 June 2013 at 11:33:29 UTC, monarch_dodra wrote:
 On Friday, 21 June 2013 at 10:09:10 UTC, 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?
Regardless of whether you end up using lots of ~=, or Appender (possibly with formattedWrite), using reserve never hurts.
It's worth pointing out that Appender supports ~= so it's very easy to swap it in, replacing builtin concatenation.
Jun 21 2013
parent "Namespace" <rswhite4 googlemail.com> writes:
 It's worth pointing out that Appender supports ~= so it's very 
 easy to swap it in, replacing builtin concatenation.
This works since 2.062 AFAIK. So is still quite new.
Jun 21 2013