@Inces Not too sure how to use the profiler to detect speed vs. memory usage performance, but below is an example of using a temporary array in an inefficient manner (I beleive you will suffer speed performance due to allocating and freeing an array) to do a summation of all products of pairs of number in a large array
var largeArray = ...
var productSum = 0
for i in largeArray.size():
for j in largeArray.size():
#purely for example. using a temprary array here is not necessary
var tmpArray = [] #allocated every time the for loop repeats
tmpArray.append(largeArray[i])
tmpArray.append(largeArray[j])
productSum = productSum + (tmpArray[0] * tmpArray[1])
Instead, you could use a single array and avoid having it be created every iteration of the for loop as follows
var largeArray = ...
var tmpArray = [] #only ever allocated once
tmpArray.append(null)
tmpArray.append(null)
var productSum = 0
for i in largeArray.size():
for j in largeArray.size():
#purely for example. using a temprary array here is not necessary
tmpArray[0]=largeArray[i]
tmpArray[1]=largeArray[j]
productSum = productSum + (tmpArray[0] * tmpArray[1])
A more practical case would be your using an array to return 2 values from a function:
func funcWith2ReturnValues():
return [123,456]
If the calling functions are only interested in reading the values returned and not populating the returned array with anything, then the below would be a more optimal solution (it would avoid creating an array every time funcWith2ReturnValues
is called
var tmpArray = [null,null]
func funcWith2ReturnValues():
tmpArray[0]=123
tmpArray[1]=456
return tmpArray