简体   繁体   English

使用 list.size() 或变量进行多次使用? (局部优化)

[英]Use list.size() or a variable for multiple use ? (local optimization)

I have a simple function called a lot.我有一个简单的 function 调用了很多。

Inside this function, I have many calls to the size of a list (containing around 10 elements):在这个 function 中,我对列表的大小(包含大约 10 个元素)有很多调用:

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 ?我使用临时变量只获取一次大小会更快,还是每次调用size()方法更快?

Update : it's an ArrayList .更新:它是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 entirely depends on the implementation.这完全取决于实施。 You haven't specified the type of list - I assume it's a List<E> or some concrete implementation.您没有指定list的类型 - 我假设它是List<E>或一些具体的实现。

In some implementations such as ArrayList<E> it's extremely cheap - a field access, basically.在诸如ArrayList<E>之类的一些实现中,它非常便宜——基本上是字段访问。 It's only documented in terms of being constant time, admittedly:诚然,它仅记录在恒定时间方面:

The size , isEmpty , get , set , iterator , and listIterator operations run in constant time. sizeisEmptygetsetiteratorlistIterator操作在恒定时间内运行。

In others it could potentially be expensive.在其他情况下,它可能会很昂贵。 The interface doesn't provide any guarantees.该接口不提供任何保证。 I would expect it to be cheap (constant time) in most implementations, but you never know for sure...我希望它在大多数实现中都很便宜(恒定时间),但你永远无法确定......

It depends on implementation of the List Looking at the ArrayList 's source这取决于查看ArrayList源的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   

So it doesn't matter if you take a local variable or call this method所以不管是取局部变量还是调用这个方法

Check this out (from ArrayList and also LinkedList ):看看这个(来自ArrayListLinkedList ):

/**
 * Returns the number of elements in this list.
 *
 * @return the number of elements in this list
 */
public int size() {
return size;
}

Calling list.size() is about as efficient as invoking a method and putting a value on the stack: (almost) negligible.调用list.size()与调用方法并将值放入堆栈一样有效:(几乎)可以忽略不计。 Of course you'll be a tiny bit faster using a local ( final ) variable.当然,使用本地( final )变量会快一点。 If this is an important improvement in the context of your application or not, you will probably have to measure.如果这在您的应用程序上下文中是否是一项重要的改进,您可能必须进行衡量。

Regardless of how fast size() method call is.不管size()方法调用有多快。 It's a good practice to introduce a variable to carry the result of the method, if that method is invoked multiple times inside a block of code, assuming that the method doesn't change anything.如果该方法在代码块中被多次调用,假设该方法没有改变任何东西,那么引入一个变量来携带该方法的结果是一个很好的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM