Associate
- Joined
- 9 Nov 2009
- Posts
- 269
I spent half an hour doing an experiment.
With Lists, everytime we add and the (natural) limit of the list is hit, the capacity, doubles. This doubling, allegedly, uses resources and wastes time. As you have stated, by resizing the array, before using the series of .add methods, should, in theory save time.
I tried this and it didnt work.
I set up a timing system and in general, I got the following results:
typical load time (when i resize the array, before using .add)..............7.2s
typical load time (when I do not resize the list, before using .add) ......7.0s
I thought that this was a little bizarre, but I have to rely on what I see and the results of my tests, as opposed to the theory. Hence, I shall not be resizing the capacity of the List, before calling .add.
How do mean resize the array? You set the init size via the ctor then add away, it's only a "performance tweak" if you know the size of the list upfront and have a large number (500k+?). If do you arguablebly you'd be better with other structures. Also, be very careful of that array.copy, copying a large number of items can be slow if you're only just over your init size where as exponential growth (the default) does a lot of smaller array copies.
I used this code (admittedly not thread safe but is good for benchmarking) and seen decent saving in large lists. Not so much when you have <1m items:
Default growth with 100 items: 0.0042 ms
Init size and growth set at 10 with 100 items: 0.0026 ms
Default growth with 500 items: 0.0114 ms
Init size and growth set at 250 with 500 items: 0.0107 ms
Default growth with 15000 items: 0.2524 ms
Init size and growth set at 1000 with 15000 items: 0.254 ms
Default growth with 12000 items: 0.2179 ms
Init size and growth set at 10000 with 12000 items: 0.2065 ms
Default growth with 12000 items: 0.2187 ms
Init size and growth set at 6000 with 12000 items: 0.1988 ms
Default growth with 27000 items: 0.4754 ms
Init size and growth set at 6000 with 27000 items: 0.49 ms
Default growth with 10100 items: 0.1942 ms
Init size and growth set at 10000 with 10100 items: 0.1927 ms
Default growth with 100000 items: 10.5534 ms
Init size and growth set at 100000 with 100000 items: 1.1964 ms
Default growth with 150000 items: 2.3558 ms
Init size and growth set at 100000 with 150000 items: 4.1924 ms
Default growth with 10000000 items: 1250.9683 ms
Init size and growth set at 9000000 with 10000000 items: 1452.7528 ms
Be aware all these are in milliseconds so this is extremely in micro optimisations category.