Dear All,
I'm searching for a fast solution in Swift for memory management. What do I mean? I need to initialise an array (or buffer) with for example 1000000 bytes, fill it with zero (0) and insert at given position in another array (or memory buffer). Then I must have a fast way to change the values for each of the elements. How can this be accomplished in Swift? Here is the array version:
1. Initialisation of a new array - there are three ways to do it. I measured the time of 10 consecutive executions of each of this methods:
Option I - time: 1.38 ms
let tmp = Array<Int8>(count: 1000000, repeatedValue: 0)
Option II - time: 385.54 ms
let tmp = Array<Int8>(Repeat(count: 1000000, repeatedValue: 0))
Option III - time: 586.43 ms
let ptr = UnsafeMutablePointer<Int8>.alloc(1000000)
ptr.initializeFrom(Repeat(count: 1000000, repeatedValue: 0))
Option II and III are borrowed from Airspeed Velocity. I would like to use this oportunity and thank him for co-authoring such a nice book ;-)
2. Insert the new array in other array at position 'x'
For Option I and II I'm using the following:
array.insertContentsOf(tmp, at: x)
The time needed for a single such operation (no matter the value of 'x') is 654.19 ms. I really don't know how to do this with Option III.
3. Change the values
The values of the new array can be changes (of course) like this:
array[13453] = 20
Any recommendations to speed up the entire process, especially the inserting (part 2), are more than welcomed. The implementation may use arrays ... pointers ... it really doesn't matter if the requirements are met.
I.