my ideas for array operations
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Mon Oct 8 01:52:49 PDT 2007
Janice Caron wrote:
> On 10/7/07, David Brown <dlang at davidb.org> wrote:
>> But, I agree, this doesn't need to be in the language, since it is easy to
>> write.
>
> It is worth pondering about efficiency though. Most especially when it
> comes to concatenation. That is:
>
> string s = cat(a,b,c,d,e,f,g,h);
>
> ought to be more efficient than
>
> string s = a~b~c~d~e~f~g~h;
>
> because the former could, in principle, require only one allocation,
> wheras the latter needs seven.
>
> The fast way:
> s.length = a.length + b.length + ...
> s[0..a.length] = a[];
> s[a.length..a.length+b.length] = b[];
> ...
>
> Order O(N)
Actually, DMD optimizes the second case by calling a special function in
the runtime library for concatenating >2 arrays. It works in your "fast
way".
For some reason GDC (0.24) seems to also call that function (even in the
2-array case) but with only two arrays at a time, defeating the purpose :(.
(I only found out about this bug while writing this post, and filed a
report: http://d.puremagic.com/issues/show_bug.cgi?id=1556)
More information about the Digitalmars-d
mailing list