哪个更快? double [][] 矩阵或 ArrayList>
在 Java 中,速度更快,请注意,我不需要 Big O 事物中的(删除、添加)灵活性。但我当然需要 Access Big O。
操作只是将 2 个矩阵相乘或减法、加法等。
还要注意,数组是原始数组。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
该数组应该更快,因为 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.
[][]
数组会更快。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.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 thedouble
indouble[]
will be continuous in memory, also improving cache performance.BTW:
Double
may be rather randomly arranged in memory and there for the cache.double[][]
会更快,因为它可以避免算术运算期间的自动装箱。double[][]
will be faster because it'll avoid autoboxing during arithmetic operations.使用
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.
双[][]更快。在java中没有比这更原始的语句了。
double [] [] is faster. In java there is no statement more primitive than that.
double[][]
和ArrayList>
上的操作将在同一个Big-O 界限。也就是说,无症状边界是相同的 - 如果 ArrayList 不必调整大小,那么索引的访问确实是
O(1)
操作(即使C
[常数]对于访问和双/双装箱和内存局部性来说可能更大)。选择其中之一不会增加或减少复杂性或改变Big-O。哪一个挂钟更快?我的赌注是在数组上,但“确定”的唯一方法 - 包括“快多少”,以及在什么情况下 - 是在给定的环境/问题/问题大小上对不同的结构进行基准测试并比较结果。 (这应该是尝试“优化”时的第一步。)
快乐编码。
The operations on
double[][]
andArrayList<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 theC
[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.