Thread overview
is char[] faster than string?
Apr 05, 2017
Inquie
Apr 05, 2017
Adam D. Ruppe
Apr 05, 2017
H. S. Teoh
Apr 07, 2017
Anonymouse
Apr 06, 2017
Kagamin
Apr 06, 2017
Gary Willoughby
April 05, 2017
I have a lot of string concatenation to do and I'm wondering if char[] is faster? Does it simply extend the buffer or are new buffers created every time?

What I am looking for is something like StringBuilder in C#.
April 05, 2017
On Wednesday, 5 April 2017 at 21:58:16 UTC, Inquie wrote:
> I have a lot of string concatenation to do and I'm wondering if char[] is faster?

No, they are the same.

> Does it simply extend the buffer or are new buffers created every time?

Both will extend the buffer when the runtime can guarantee it is allowed to, which it quite often can. Details here: http://dlang.org/d-array-article.html


I recommend just trying it with ~= (avoid a ~ b though, a ~= b is better when possible) and see how it performs before getting too worried about it, the built in really isn't bad.
April 05, 2017
On Wed, Apr 05, 2017 at 09:58:16PM +0000, Inquie via Digitalmars-d-learn wrote:
> I have a lot of string concatenation to do and I'm wondering if char[] is faster? Does it simply extend the buffer or are new buffers created every time?
> 
> What I am looking for is something like StringBuilder in C#.

If you are doing lots of concatenation and produce a single big string at the end, take a look at std.array.appender.

Though if you're concerned about performance, you really should run a profiler. Last I heard, appender may not be that much faster than using ~=, but I could be wrong.  But when it comes to optimization, my advice is, profile, profile, profile.  I came from a C/C++ background and used to have all sorts of zany ideas about optimization, but eventually I learned that 95% of the time my efforts were wasted because the real bottleneck was somewhere else, usually in an unexpected place (that only made sense in retrospect).  Always use a profiler before making decisions on optimizations.  It will save you from a lot of headaches and unwarranted optimizations that tend to make your code needlessly convoluted.


T

-- 
Latin's a dead language, as dead as can be; it killed off all the Romans, and now it's killing me! -- Schoolboy
April 06, 2017
On Wednesday, 5 April 2017 at 21:58:16 UTC, Inquie wrote:
> What I am looking for is something like StringBuilder in C#.

If you want it to not copy data on expand, there's nothing like that in D yet. I wrote one for myself :)
April 06, 2017
On Wednesday, 5 April 2017 at 21:58:16 UTC, Inquie wrote:
> What I am looking for is something like StringBuilder in C#.

std.array.appender
April 07, 2017
On Wednesday, 5 April 2017 at 22:05:07 UTC, H. S. Teoh wrote:
> If you are doing lots of concatenation and produce a single big string at the end, take a look at std.array.appender.
>
> Though if you're concerned about performance, you really should run a profiler. Last I heard, appender may not be that much faster than using ~=, but I could be wrong.

If my understanding serves, and it's very likely that it doesn't, then it works precisely as normally appending does but keeps track of array capacity on its own, so the GC doesn't have to do (expensive?) queries upon every append.

> But when it comes to optimization, my advice is, profile, profile, profile.

This. valgrind --tool=callgrind, ddemangle and QCachegrind are your best friends.