I've come across an interesting Swift performance problem, and was looking for some suggestions, analysis on why this is happening.
I have an algorithm that required hundreds of thousands of array accesses in a loop. I find that if I reference the array as an instance property (from inside the same class instance), the performance is very poor. It seems that the array is being de-referenced at each iteration. That seems strange given that the arrays are members of the same class doing the work. Wouldn't self.x not require x to be dereferenced over and over again? The equivalent Java code doesn't have the same performance problem.
In the below example, test3 takes 0.5 seconds and test4 takes 0.15 seconds.
Do I really have to go through all my code and assign locally scoped arrays every single time I do something?
Any tips/ideas would be welcome. I have the compiler optimization set to Fast-O.
Simon
EDIT: The answer is spelled out in this article here: https://developer.apple.com/swift/blog/?id=27
Hope it helps. Long story short, private/final for the class scoped variables will remove the need for the unwanted indirection to access the array.
class MyClass {
var array_1 = [Int64] (count: 16 , repeatedValue: 0)
var array_2 = [Int64] (count: 16 , repeatedValue: 0)
func runTest3() {
// test #3
//
let start = NSDate().timeIntervalSince1970
for i in 0 ... 10000000 {
if (array_1[ i%16 ] & array_2[ i%16 ] ) != 0 {
// whatever
}
}
let passed = NSDate().timeIntervalSince1970 - start
print("3 time passed: \(passed)")
}
func runTest4() {
// test #4
//
let start = NSDate().timeIntervalSince1970
let localArray_1 = self.array_1
let localArray_2 = self.array_2
for i in 0 ... 10000000 {
if (localArray_1[ i%16 ] & localArray_2[ i%16 ] ) != 0 {
// whatever
}
}
let passed = NSDate().timeIntervalSince1970 - start
print("4 time passed: \(passed)")
}
}
-Oand got less than 0.01 seconds for each one. It probably depends what you do inside the loop.