Java 中的 get() 或 elementAt()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7141445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get() or elementAt() in Java
提问by Zeeshan Siddiqui
Which is faster and takes less amount of time (in nanoseconds) in JAVA between the two?
两者之间在JAVA中哪个更快并且花费的时间更少(以纳秒为单位)?
get()
or elementAt()
get()
或者 elementAt()
I'm using them to return element stored in an object. And speed is critical, therefore I wanted to know which is more feasible and faster amongst the two.
我正在使用它们来返回存储在对象中的元素。速度至关重要,因此我想知道两者中哪个更可行、更快。
public E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return get(len - 1);
}
or
或者
public E peek() {
int len = size();
if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
}
回答by debracey
First off, you didn't tell us what data structure you are working with. So, I'll go with the assumption that you are using Vector
or some Vector
derivative.
首先,您没有告诉我们您正在使用什么数据结构。因此,我将假设您正在使用Vector
或使用某些Vector
衍生产品。
The two methods are identical according to the documentation:
根据文档,这两种方法是相同的:
http://download.oracle.com/javase/1,5.0/docs/api/java/util/Vector.html
http://download.oracle.com/javase/1,5.0/docs/api/java/util/Vector.html
That being said however, elementAt(idx)
, dates back to the days when Vector did not follow the List patterns (by extending AbstractList
) -- if you read the full docs you'll see that Vector was modified to implement the List interfaces.
然而,话虽如此,elementAt(idx)
可以追溯到 Vector 不遵循 List 模式(通过扩展AbstractList
)的日子——如果您阅读完整的文档,您会看到 Vector 已被修改以实现 List 接口。
Therefore, I would expect get(idx)
to provide the fastest speeds, and elementAt(idx)
to simply call through to get(idx)
. At any rate, the difference in speed is going to be almost nothing -- and you should look elsewhere if you're looking to get a performance bump.
因此,我希望get(idx)
提供最快的速度,并elementAt(idx)
简单地调用get(idx)
. 无论如何,速度上的差异几乎为零——如果您希望获得性能提升,您应该寻找其他地方。
回答by Lew Bloch
This is a classic example of premature optimization and micro-optimization that will not actually optimize anything. Even if the methods were different you wouldn't be able to tell the difference ahead of time without measurement on the target hardware under realistic loads. Java runtimes have a sneaky way of optimizing code for you based on actual run-time conditions, so generalization at that level of detail is pretty much useless.
That said, you probably should not use 'Vector' as the class, because unless you need it (do you?) the method-level synchronization adds (small) overhead compared to 'ArrayList'. 'Vector' has been out-of-date since '98.
The rule of thumb is, "First make it right, then make it fast." In this case, using 'get()' makes it right and 'elementAt()' makes it wrong. If you treat your type as 'List' you don't get locked into having to use 'Vector' particularly; you have more freedom to change implementation, such as from 'ArrayList' to 'CopyOnWriteArrayList()' or 'LinkedList', depending on the needs of program logic. It doesn't do you much good to shave a microsecond off a call to get the wrong answer, or to lose the ability to use a pre-packaged solution that's better than what you have time to develop. So just use 'get()'.
这是过早优化和微优化的经典例子,实际上不会优化任何东西。即使方法不同,如果不在实际负载下对目标硬件进行测量,您也无法提前分辨出差异。Java 运行时有一种基于实际运行时条件为您优化代码的偷偷摸摸的方法,因此在该细节级别上的泛化几乎毫无用处。
也就是说,您可能不应该使用“Vector”作为类,因为除非您需要它(是吗?),与“ArrayList”相比,方法级同步会增加(小)开销。“矢量”自 98 年以来已经过时。
经验法则是,“先做对,再做快”。在这种情况下,使用 'get()' 使其正确,而使用 'elementAt()' 使其错误。如果您将您的类型视为“列表”,您就不会陷入必须特别使用“矢量”的困境;根据程序逻辑的需要,您可以更自由地更改实现,例如从“ArrayList”更改为“CopyOnWriteArrayList()”或“LinkedList”。将电话缩短一微秒以获得错误答案,或者失去使用比您有时间开发的解决方案更好的预打包解决方案的能力,这对您没有多大好处。所以只需使用'get()'。
Oh, and if you are hand-implementing 'peek()' that's another mistake. Use an API class like one of the 'Deque' implementations that comes with the SDK. http://download.oracle.com/javase/7/docs/api/java/util/Deque.html
哦,如果您手动实现 'peek()',那又是一个错误。使用 API 类,例如 SDK 附带的“Deque”实现之一。 http://download.oracle.com/javase/7/docs/api/java/util/Deque.html
回答by Luke Morgan
The implementation is the same, at least in the source of OpenJDK 7. Both return elementData(index); after checking if the index is in the bounds.
实现是一样的,至少在OpenJDK 7的源码中是一样的。两者都返回elementData(index);在检查索引是否在边界内之后。