is char[] faster than string?

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 5 15:05:07 PDT 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


More information about the Digitalmars-d-learn mailing list