I was looking at my Swift Gray Scott application last night and started playing with different ways I could improve the speed of the solvers.
I started tinkering and found some unexpected results:
- It is quicker to populate the values of an Array by first giving it a length and some arbitrary value (e.g vararrayOne : Array<Int> = Array<Int>(count: count, repeatedValue : 0); ) and then setting your required value with subscripting than creating an empty array and adding values with append()).
- If you are looping over the values of an array, depending on the length of the array, it is often quicker to create a const copy of the array and loop over that. However, in my example project as the array approaches 20,000 items, it's quicker to loop over the original array.
- NSMutableArray is far, far quicker that Array - even if you have to cast its members - and populating withaddObject() is no quicker that using subscripting.
- The creating a constant copy technique also works for NSMutableArray - but seems to favour larger arrays over smaller ones (i.e. the opposite of Array).