Populating swift two dimensional array using append vs element by element
Using Apple Swift: I have a very large 2D String array from, for example, a .cdl file. Then I create a Double array from a String array, so now I have a String array (variableData) and a double array (variableDataDouble). Now I want to customize the values ββin the Double array.
If I use append and a String array, the setup is fast.
cdlVariablesDictionary[eachVariable]?.variableDataDouble.removeAll()
for rows in 0 ..< rowsCount {
var tempArray: [Double] = []
for columns in 0 ..< columnsCount {
var adjustedValue: Double = Double((cdlVariablesDictionary[eachVariable]?.variableData[rows][columns])!)!
adjustedValue = adjustedValue * scaleNumeric + offsetNumeric
tempArray.append(adjustedValue)
}
cdlVariablesDictionary[eachVariable]?.variableDataDouble.append(tempArray)
}
If I adjust each value in the Double array, the adjustment is very slow.
for rows in 0 ..< rowsCount {
for columns in 0 ..< columnsCount {
var adjustedValue: Double = (cdlVariablesDictionary[eachVariable]?.variableDataDouble[rows][columns])!
adjustedValue = adjustedValue * scaleNumeric + offsetNumeric
cdlVariablesDictionary[eachVariable]?.variableDataDouble[rows][columns] = adjustedValue
}
}
Why is append fast and element-to-element very, very slow?
+3
source to share
No one has answered this question yet
Check out similar questions: