Java 性能 - ArrayLists 与 Arrays 的大量快速读取 [英] Java Performance - ArrayLists versus Arrays for lots of fast reads

查看:24
本文介绍了Java 性能 - ArrayLists 与 Arrays 的大量快速读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,对于类似细胞自动机的程序,我需要在尽可能短的时间内(以毫秒为单位)对类似 List 的对象进行 100,000 到 1,000,000 次随机访问读取.我认为我正在使用的更新算法已经优化(有效地跟踪活动单元格等).列表确实需要更改大小,但性能并不重要.所以我想知道使用 Arrays 而不是 ArrayLists 的性能是否足以在如此短的时间内处理如此多的读取.目前,我正在使用 ArrayLists.

I have a program where I need to make 100,000 to 1,000,000 random-access reads to a List-like object in as little time as possible (as in milliseconds) for a cellular automata-like program. I think the update algorithm I'm using is already optimized (keeps track of active cells efficiently, etc). The Lists do need to change size, but that performance is not as important. So I am wondering if the performance from using Arrays instead of ArrayLists is enough to make a difference when dealing with that many reads in such short spans of time. Currently, I'm using ArrayLists.

我忘了提及:我只是存储整数,所以另一个因素是使用 Integer 包装类(在 ArrayLists 的情况下)与 ints(在数组的情况下).有谁知道使用 ArrayList 是否实际上需要 3 次指针查找(一次用于 ArrayList,一次用于底层数组,另一次用于 Integer->int),其中数组只需要 1 次(数组地址+偏移量到特定内部)?HotSpot 会优化额外的查找吗?这些额外的查找有多重要?

I forgot to mention: I'm just storing integers, so another factor is using the Integer wrapper class (in the case of ArrayLists) versus ints (in the case of arrays). Does anyone know if using ArrayList will actually require 3 pointer look ups (one for the ArrayList, one for the underlying array, and one for the Integer->int) where as the array would only require 1 (array address+offset to the specific int)? Would HotSpot optimize the extra look ups away? How significant are those extra look ups?

另外,我忘了提到我还需要进行随机访问写入(写入,而不是插入).

Also, I forgot to mention I need to do random access writes as well (writes, not insertions).

推荐答案

既然您已经提到您的数组实际上是原始类型的数组,请考虑使用 Trove 库.

Now that you've mentioned that your arrays are actually arrays of primitive types, consider using the collection-of-primitive-type classes in the Trove library.

@viking 报告了在他的应用程序中使用 Trove 的显着(十倍!)加速 - 请参阅评论.另一方面是 Trove 集合类型与 Java 的标准集合 API 的类型不兼容.所以 Trove(或类似的库)不会在所有情况下都是答案.

@viking reports significant (ten-fold!) speedup using Trove in his application - see comments. The flip-side is that Trove collection types are not type compatible with Java's standard collection APIs. So Trove (or similar libraries) won't be the answer in all cases.

这篇关于Java 性能 - ArrayLists 与 Arrays 的大量快速读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆