iOS Swift Multiple Dimension Arrays - Compiliing Takes Ages. What Should I Change

Swift Array of Ranges causing issues

Declare your variable as an array of arrays of Int

let arrayOfRanges: [[Int]] = [Array(0 ... 299), Array(300 ... 399), Array(400 ... 699), Array(700 ... 799), Array(800 ... 899), Array(900 ... 1199)]
let magicNumber = arrayOfRanges[2][11]

xy array in Swift performance

I checked the performance for various solutions to your problem. You can download my tests from this link to github

A) Your code

var spectrum: [XYPoint] = []
for i in 0..<xArray.count {
let xy = XYPoint(x: xArray[i], y: yArray[i])
spectrum.append(xy)
}

B) Zip + map (Martin R's answer)

let spectrumB = zip(xArray, yArray).map(XYPoint.init)

C) Range + map (My solution)

let spectrum = (0 ..< xArray.count).map { i in
return XYPoint(x: xArray[i], y: yArray[i])
}

D) ReserveCapacity + append (Duncan C's answer)

var spectrum: [XYPoint] = []
spectrum.reserveCapacity(xArray.count)
for (index, value) in xArray.enumerated() {
spectrum.append(XYPoint(x: xArray[index], y: yArray[index]))
}

My results (in seconds)

            ╭──────────────┬──────────────┬──────────────┬──────────────╮
│ A │ B │ C │ D │
╭───────────╬══════════════╪══════════════╪══════════════╪══════════════╡
│ 100 ║ 0.000009426 │ 0.000002401 │ 0.000000571 │ 0.000000550 │
│ 200 ║ 0.000003356 │ 0.000002629 │ 0.000000911 │ 0.000000866 │
│ 500 ║ 0.000005610 │ 0.000007288 │ 0.000002236 │ 0.000002012 │
│ 1000 ║ 0.000010638 │ 0.000009181 │ 0.000003905 │ 0.000005030 │
│ 2000 ║ 0.000019377 │ 0.000013316 │ 0.000007116 │ 0.000008732 │
│ 5000 ║ 0.000023430 │ 0.000019304 │ 0.000019809 │ 0.000019092 │
│ 10000 ║ 0.000050463 │ 0.000031669 │ 0.000035121 │ 0.000035420 │
│ 20000 ║ 0.000087040 │ 0.000058664 │ 0.000069300 │ 0.000069456 │
│ 50000 ║ 0.000272357 │ 0.000204213 │ 0.000176962 │ 0.000192996 │
│ 100000 ║ 0.000721436 │ 0.000459551 │ 0.000415024 │ 0.000437604 │
│ 200000 ║ 0.001114534 │ 0.000924621 │ 0.000816374 │ 0.000896202 │
│ 500000 ║ 0.002576687 │ 0.002094998 │ 0.001860833 │ 0.002060462 │
│ 1000000 ║ 0.007063596 │ 0.005924892 │ 0.004319181 │ 0.004869024 │
│ 2000000 ║ 0.014474969 │ 0.013594134 │ 0.008568550 │ 0.009388957 │
│ 5000000 ║ 0.038348767 │ 0.035136008 │ 0.021276415 │ 0.023855382 │
│ 10000000 ║ 0.081750925 │ 0.078742713 │ 0.043578664 │ 0.047700495 │
│ 20000000 ║ 0.202616669 │ 0.199960563 │ 0.148141266 │ 0.145360923 │
│ 50000000 ║ 0.567078563 │ 0.552158644 │ 0.370327555 │ 0.397115294 │
│ 100000000 ║ 1.136993625 │ 1.101725386 │ 0.713406642 │ 0.740150322 │
└───────────╨──────────────┴──────────────┴──────────────┴──────────────┘

Compiling large array literal in Xcode 8.2

I'm sorry to keep making the same joke, but you need to do this the same way Superman gets into his pants: one leg at a time. Start with the overall structure. Then keep making the innermost arrays and appending them into the outer arrays to build up the outermost array. This will get you started:

var level1 : [[[[Int]]]] = []
var level2 : [[[Int]]] = []
var level3 : [[Int]] = []
// -----
level3 = []
let innermost1 = [1,2,3,4,5,6,7,8,9]
let innermost2 = [11,12,13,14,15,16,17,18,19]
level3.append(innermost1)
level3.append(innermost2)
level2.append(level3)
level1.append(level2)
// ... keep going ...

In this way, we never need any big / deep array literals, and the project will compile easily.

Note too that the type of the array at each level is explicitly declared. This makes Swift a lot happier than having to infer it.



Related Topics



Leave a reply



Submit