I have a simple function called a lot.
Inside this function, I have many calls to the size of a list (containing around 10 elements):
list.size()
Is it faster for me to use a temporary variable to get the size only once, or is it faster to call the size() method every time?
Update: it’s an ArrayList.
Note: I know what I am doing, I am not looking for a lecture regarding optimization and how it should or shouldn’t be done. I am just looking for an answer.
It depends on implementation of the
ListLooking at the
ArrayList‘s sourceSo it doesn’t matter if you take a local variable or call this method