4

    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.

4
  • 1
    Did you turn on compiler's optimization while measuring? Commented Aug 1, 2016 at 22:01
  • And second question - what is the intent of using large collections? If you're trying to do i.e. image processing than CoreGraphics would do this job better Commented Aug 1, 2016 at 22:06
  • Optimisation helped. What a silly mistake. What is the conclusion - don't code too much ... you will start losing your senses. Please make it as an answer so I can accept it. Commented Aug 2, 2016 at 6:16
  • Btw I'm not manipulation images, but simply saving values, which I'll need later. Can you recommend a better way ... maybe using CG, as you suggested? Commented Aug 2, 2016 at 18:25

1 Answer 1

1

Turn on optimization during measuring - Swift's performance is all about it.

Sign up to request clarification or add additional context in comments.

3 Comments

Years ago ... when I started programming, it was faster to access variable via pointer and not via an array. Do you have any idea how I can accomplish this in swift? It's really slow to access 10-20 Mbytes of an array ..... Please recommend something faster.
Yes optimization makes a difference. Zeroing an array with 16,737,732 bytes, in my test case, went from 2.37 seconds (no optimization) to 0.0165 seconds (optimized). THAT'S 143 TIMES FASTER! Btw, took C's memset only 0.00827seconds on the same machine -- that' another 2x speedup. I am not sure Swift is a good language for fast image processing on the CPU.
I'm interested what was the version of Swift you used in your tests - 3 or 4 ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.