www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is char[] faster than string?

reply Inquie <Inquie data.com> writes:
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#.
Apr 05
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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.
Apr 05
prev sibling next sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
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
Apr 05
parent Anonymouse <asdf asdf.com> writes:
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.
Apr 07
prev sibling next sibling parent Kagamin <spam here.lot> writes:
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 :)
Apr 06
prev sibling parent Gary Willoughby <dev nomad.so> writes:
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
Apr 06