阵列VS多维数组的数组的性能比较 [英] Performance comparison of array of arrays vs multidimensional arrays

查看:181
本文介绍了阵列VS多维数组的数组的性能比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用C ++在大学里,我被告知尽量使用多维数组(特此MDA),因为它表现出更好的内存位置,因为它在一大块的分配。阵列(AOA)的数组,而另一方面,被分配在多个较小的块,可能散落各地的空缺职位的地方被发现的物理内存的地方。

When I was using C++ in college, I was told to use multidimensional arrays (hereby MDA) whenever possible, since it exhibits better memory locality since it's allocated in one big chunk. Array of arrays (AoA), on the other hand, are allocated in multiple smaller chunks, possibly scattered all over the place in the physical memory wherever vacancies are found.

所以我想第一个问题是:这是一个神话,或者这是值得以下内容的通知

So I guess the first question is: is this a myth, or is this an advice worth following?

假如是后者,那么接下来的问题是如何在Java等语言,没有真正做到MDA。这并不难效仿MDA与1DA,当然。从本质上讲,是与MDA语言的语法糖可以实现为不为MDA语言库的支持。

Assuming that it's the latter, then the next question would be what to do in a language like Java that doesn't have true MDA. It's not that hard to emulate MDA with a 1DA, of course. Essentially, what is syntactic sugar for languages with MDA can be implemented as library support for languages without MDA.

这是值得的?是一种优化问题对像Java语言这个等级太低?如果我们只是放弃阵列,并使用列表即使原语S'

Is this worth the effort? Is this too low level of an optimization issue for a language like Java? Should we just abandon arrays and use Lists even for primitives?

另一个问题:在Java中,并在一次分配迎角(新INT [M] [N] )可能产生比分层做(<$不同的内存分配C $ C>新INT [M] [];吗?(...新的INT [n] )

Another question: in Java, does allocating AoA at once (new int[M][N]) possibly yield a different memory allocation than doing it hierarchically (new int[M][]; for (... new int[N])?

推荐答案

Java和C#在很多不同的方式是C ++不分配内存。事实上,在.NET肯定的农业协定中的所有阵列将接近在一起,如果他们被陆续分配之一,因为内存只有一个连续的块不用任何碎片。

Java and C# allocate memory in much different fashion that C++ does. In fact, in .NET for sure all the arrays of AoA will be close together if they are allocated one after another because memory there is just one continuous chunk without any fragmentation whatsoever.

但它仍然是对C ++如此,仍然是有意义的,如果你想最大速度。虽然你不应该按照您希望多维数组每次提醒,你应该先写维护code,然后配置文件,如果它是缓慢的,premature优化是在这个世界上所有的罪恶根源。

But it is still true for C++ and still makes sense if you want maximum speed. Although you shouldn't follow that advise every time you want multidimensional array, you should write maintainable code first and then profile it if it is slow, premature optimization is root for all evil in this world.

这篇关于阵列VS多维数组的数组的性能比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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