哪个更快? double [][] 矩阵或 ArrayList>

发布于 2024-12-09 21:54:57 字数 123 浏览 1 评论 0 原文

在 Java 中,速度更快,请注意,我不需要 Big O 事物中的(删除、添加)灵活性。但我当然需要 Access Big O。

操作只是将 2 个矩阵相乘或减法、加法等。

还要注意,数组是原始数组。

In Java which is faster note that I don't need the flexibility of (remove, add)in Big O thing. But I certainly need the Access Big O.

The operation is only to multiply 2 matrices or subtract, add etc..

Also note that the array is primitive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

无声无音无过去 2024-12-16 21:54:58

该数组应该更快,因为 ArrayLust 还使用内部数组,因此您需要额外的调用 (get(x).get(y)),这将消耗更多时间。

The array should be faster as the ArrayLust uses also an internal array and therefor you have an additional call (get(x).get(y)) which will consume further time.

享受孤独 2024-12-16 21:54:58

[][] 数组会更快。 ArrayList 访问本质上同样快(它在内部使用 []),但需要额外的基于对象的工作(它必须查看两个对象而不是一个数组,例如)会使其稍微慢一些。

The [][] array would be faster. ArrayList access is essentially just as fast (it uses [] internally) but the extra work object-based work needed (it has to look into two objects instead of one array, for example) would make it slightly slower.

故笙诉离歌 2024-12-16 21:54:57

double[][] 比使用 ArrayList 和 Double 的内存效率更高。它将使用一小部分内存,这意味着您将获得更好的缓存行为。另外,double[]中的double将在内存中连续,也提高了缓存性能。

顺便说一句:Double 可能会相当随机地排列在内存和缓存中。

double[][] is much more memory efficient than using ArrayLists and Double. It will use a fraction of the memory meaning you will get better caching behaviour. Also the double in double[] will be continuous in memory, also improving cache performance.

BTW: Double may be rather randomly arranged in memory and there for the cache.

魄砕の薆 2024-12-16 21:54:57

double[][] 会更快,因为它可以避免算术运算期间的自动装箱。

double[][] will be faster because it'll avoid autoboxing during arithmetic operations.

不可一世的女人 2024-12-16 21:54:57

使用double [][],您无需担心自动装箱或内部调整大小/复制操作,因此速度会更快。

另一方面,除非您正在处理非常大的集合,否则性能差异应该不明显。

With double [][] you won't need to worry about Autoboxing or internal resizing/copying operations, therefore it will be faster.

On the other hand, the performance difference should be unnoticeable unless you're working with extremely large collections.

勿挽旧人 2024-12-16 21:54:57

双[][]更快。在java中没有比这更原始的语句了。

double [] [] is faster. In java there is no statement more primitive than that.

伤感在游骋 2024-12-16 21:54:57

double[][]ArrayList> 上的操作将在同一个Big-O 界限

也就是说,无症状边界是相同的 - 如果 ArrayList 不必调整大小,那么索引的访问确实是 O(1)操作(即使C [常数]对于访问双/双装箱内存局部性来说可能更大)。选择其中之一不会增加或减少复杂性或改变Big-O。

哪一个挂钟更快?我的赌注是在数组上,但“确定”的唯一方法 - 包括“快多少”,以及在什么情况下 - 是在给定的环境/问题/问题大小上对不同的结构进行基准测试并比较结果。 (这应该是尝试“优化”时的第一步。)

快乐编码。

The operations on double[][] and ArrayList<ArrayList<Double>> will run in the same Big-O bounds.

That is, the asymptomatic bounds are the same -- if an ArrayList does not have to resize then access is truly O(1) for index operations (even though if the C [constant] might be larger for the access and the double/Double boxing and memory-locality). Choosing one over the other will not increase or reduce the complexity or change the Big-O.

Which one is wall-clock faster? My bets are on the array, but the only way "to know for certain" -- including "how much faster", and in which cases -- is to benchmark the different structures on the given environment/problem/problem size and compare the results. (This should be one of the first steps when trying to "optimize".)

Happy coding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文