java - 为高性能数据结构定义一组基本规则 (java)

标签 java performance collections guava

我通常交替使用 vector/数组列表、 HashMap / TreeMap 和其他 Java 集合,但有时会有功能性 API 要求(例如,在某些情况下我可能需要排序的数据集)。

然而,最近我发现需要将 Java 性能推到我正在运行的某些算法的极限。

是否有一套适用于高性能数据结构的指南,我可以将其用作编码的基本规则?

我正在寻找一般规则,但在这种情况下,对以下问题的回答也可能非常有帮助:

1) When should I use multidimensional arrays instead of nested Collections ?

2) Vectors vs. ArrayLists - is there truly a performance difference ?

3) Do collection API's like Google's collections, java tricks (like reflection and casting), and other common java developer idioms tend to slow down the JVM when it is under heavy load ?

4) Do primitives vs regular objects (i.e. Double vs double) slow down the JVM when doing lots of calculations ?

5) Are there other important guidelines for dealing with large collections in java programs which need to be high-performance ?

  • 注意:此时,我没有进行任何多线程处理...我意识到一旦我开始并行化,可能还会有其他约束。

最佳答案

所有性能问题都应首先通过分析(时间和内存/对象使用)来解决。不要优化不是影响代码性能的因素。有了这个警告,有一些一般的经验法则(所有这些都应该通过分析来测试!)

1) When should I use multidimensional arrays instead of nested Collections ?

当您不需要动态调整集合的大小并且不需要将数据提供给任何需要集合的东西时,多维数组(实际上是数组的数组)可能会更快。

2) Vectors vs. ArrayLists - is there truly a performance difference ?

是的。 Vector 中的许多方法都是同步的,这是昂贵的。如果您不是多线程,则避免使用 Vector。即使你是,同步的粒度通常是错误的,你最好自己提供线程安全。

3) Do collection API's like Google's collections, java tricks (like reflection and casting), and other common java developer idioms tend to slow down the JVM when it is under heavy load ?

反射很慢;垃圾收集很慢。您可以采取任何措施来避免这些情况,这会加快速度。

4) Do primitives vs regular objects (i.e. Double vs double) slow down the JVM when doing lots of calculations ?

是的。自动装箱/拆箱会很快产生大量垃圾。这一切都必须收集,这也会减慢您的程序。

5) Are there other important guidelines for dealing with large collections in java programs which need to be high-performance ?

优先使用局部方法变量而不是字段访问。您可以通过搜索网络找到许多其他指南。不过,最主要的是分析。

编辑:有很多性能提示 here .

关于java - 为高性能数据结构定义一组基本规则 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8173938/

相关文章:

c# - 对于大量自定义对象集合,Intersection() 和 Except() 太慢

c# 按列字母顺序对列表进行排序

java - String replaceAll 不替换 i++;

mysql - 使用 MySQL 分区来加速并发删除和选择?

java - C# 和 java 枚举构造函数之间有什么区别

CSS 性能 - 分组还是不分组?

mysql - 我应该为我的大学使用社交网络应用程序的共享服务器吗?

java - Scala - 如何重构方法以使用reduceLeft而不是迭代?

java - 保证将多条消息传递到 Kafka 集群

java - Spring Security 撤销权限