Tips and Tricks for D
Deewiant
deewiant.doesnotlike.spam at gmail.com
Mon Jul 3 08:19:18 PDT 2006
mclysenk at mtu.edu wrote:
> In article <e8ajmp$o9c$1 at digitaldaemon.com>, Deewiant says...
>> mclysenk at mtu.edu wrote:
>>> You can read it at http://www.assertfalse.com/articles/tricks.shtml .
>>>
>>> Any comments, questions, suggestions or other feedback is welcome!
>>>
>> I've always removed items from an array whilst preserving order in the following
>> way (using variables from your example):
>>
>> queue = queue[0..idx] ~ queue[idx+1..$];
>>
>> I did some test runs (benchmark source can be found at bottom of message), and
>> on my machine, at least, my version is consistently faster - it runs in about
>> 60% of the time yours takes.
>>
>
> First of all, thanks for the feedback! And second, you do have a good point.
> However, there is a slight bias in your test. Instead of removing elements from
> arbitrary positions in the array, you are strictly removing elements from the
> first 10000 positions. In your method, you are copying the beginning of the
> array, whereas in my method I am copying the end of the array. If you try the
> following benchmark, my version runs about 25-40 times faster on my machine:
My bad: originally I used a LENGTH of 10000 and then I neglected to change
removeThese to include more testcases.
> In the end, there isn't a single best answer to this problem. Both approaches
> have their merit, and perhaps an adaptive strategy which combines the two might
> best. (However yours performs far more consistently than mine, which may be a
> big advantage in some cases.)
>
You're right. Your method is a lot faster for removing elements near the end of
an array.
More information about the Digitalmars-d-learn
mailing list