Fastest Way to Append Multiple Elements to an Array

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Dec 19 14:43:11 PST 2014


On 12/19/14 5:28 PM, Anonymous wrote:
>>
>> What a big surprise. If you make an array of struct, each item of your
>> array has the length of the struct. structs a values.
>> If you want to append a struct to an array just append a pointer to
>> the struct:
>>
>> ----------
>> struct arg{uint a,r,g,h;};
>>
>> arg * [] argh;
>> arg [] argv;
>>
>> foreach(immutable i; 0..1000)
>>   argh ~= new arg;
>> ----------
>>
>> so in argh, each item is a size_t pointer. damn.
>> In argv, the delta between each item is
>> (a.sizeof+r.sizeof+g.sizeof+h.sizeof)
>> In argh, the delta between each item is (arg *).sizeof
>
> argv needs to be a contiguous thing so it's slower to append because
> it's not just storing a reference but the whole thing. There is
> consequently more realloc().
> argh is faster because it stores the addresses of the items.
>
> argv appends maybe 33 bytes, 33 bytes and so on.
> argh always appens 4 4 4... (32 bits OS) or 8 8 8...(64 bits OS). It's
> faster , always aligned...page-aware.that's all.
>
> Oh Oh oh.

I'm not sure what you are saying here, sorry.

-Steve


More information about the Digitalmars-d-learn mailing list