Java – use list Multiple use of size () or variable? (local optimization)

I have a simple function called many

In this function, I have many call list sizes (including about 10 elements):

list.size()

Is it faster for me to use temporary variables to get the size once, or is it faster to call the size () method every time?

Update: it is an ArrayList

Note: I know what I'm doing. I'm not looking for a speech about optimization and how it should or shouldn't be done I'm just looking for the answer

Solution

It depends on the implementation of the list

/**
  225        * Returns the number of elements in this list.
  226        *
  227        * @return the number of elements in this list
  228        */
  229       public int size() {
  230           return size;
  231       }
  232

Therefore, it is not important if you use local variables or call this method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>