I couldn't find anything regarding this so if I'm asking a question already answered I apologise.
When it comes to programming, in general, would it be better performance-wise to call a getter method a couple of times in the same function to achieve the desired outcome, or should one rather call the getter once and store the value it returned?
For example, if I have this method to do a binary search:
n ← size of the array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while true
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if getArray()[midPoint] < x
set lowerBound = midPoint + 1
if getArray()[midPoint] > x
set upperBound = midPoint - 1
if getArray()[midPoint] = x
EXIT: x found at location midPoint
end while
Or should it rather store the array as follows:
A ← getArray()
n ← size of the array
x ← value to be searched
Set lowerBound = 1
Set upperBound = n
while true
if upperBound < lowerBound
EXIT: x does not exists.
set midPoint = lowerBound + ( upperBound - lowerBound ) / 2
if A[midPoint] < x
set lowerBound = midPoint + 1
if A[midPoint] > x
set upperBound = midPoint - 1
if A[midPoint] = x
EXIT: x found at location midPoint
end while
Thanks in advance!