View mode: basic / threaded / horizontal-split · Log in · Help
May 12, 2005
Re: Array concatenation, missing functionality
Your answer makes me really happy :-)))

Tamas

In article <d5v1v7$2nr3$1@digitaldaemon.com>, Walter says...
>
>"MIcroWizard" <MIcroWizard_member@pathlink.com> wrote in message
>news:d5odll$1b1a$1@digitaldaemon.com...
>> Is there any theoretical objection against this functionality or
>> it is only not implemented yet?
>
>It's just not implemented yet. There's no technical problem with it.
May 12, 2005
Re: Array concatenation, missing functionality
In article <d5rpf7$28pr$1@digitaldaemon.com>, B.G. says...
>
>Btw, what's the current behaviour, if I set a length of an array to a 
>smaller value? Does it cause reallocation?

I'm pretty sure it does not.  So you can kind of fake the idea of a capacity
property by setting length to something large and then reducing it again.

>Does .dup return a copy where length == capacity?

I believe so.


Sean
May 12, 2005
Re: Array concatenation, missing functionality
>> Btw, what's the current behaviour, if I set a length of an array to a
>> smaller value? Does it cause reallocation?
>
> I'm pretty sure it does not. So you can kind of fake the idea of a  
> capacity property by setting length to something large and
> then reducing it again.

No, that isn't a very good solution, because it is very slow.

>> Does .dup return a copy where length == capacity?
>
> I believe so.

Not in every case.

I made some tests under linux, and the GC seems to round up the size of  
the array to a multiple of 16 bytes. If you declare this:

uint[] array;
array.length = 5;

Then you can increase the length up to 8 without reallocation. Of course  
this is not enough "preallocation" if you deal with a noteworthy number of  
elements. Even if you increase the length and set it back (MinTL does that  
with the reserve() function, i think), this variant is much slower than it  
could be. For comparison:

# array.length = 50000;
# for (int i = 0; i < 50000; ++i)
#   array[i] = i;

Takes 2.5 milliseconds.

# array.length = 50000;
# array.length = 1;
# for (int i = 0; i < 50000; ++i)
# {
#   array.length = array.length + 1;
#   array[i] = i;
# }

Takes 6 milliseconds.

# array.length = 16;
# for (int i = 0; i < 50000; ++i)
# {
#   if (i >= array.length)
#     array.length = array.length * 2;
#
#    array[i] = i;
# }
# array.length = 50000;

Takes 5 milliseconds.

As you can see, a combination of the first and the third approach will  
easily outperform the second.

Ciao
uwe
Next ›   Last »
1 2
Top | Discussion index | About this forum | D home