现代语言和数组表示法约定
您是否知道任何现代语言中数组被标记为列/行而不是 C++/Java/C# 中的行/列?
Do you know any modern language where arrays are noted as column/row instead of as it is in C++/Java/C# row/column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些语言都没有与数组相关的行或列的概念。只有程序员将这些概念与数组的一个维度或另一个维度相关联。您可以自由地互换它们,并将“行”或“列”存储在数组的任何部分。
None of those languages have the concept of a row or column in association with arrays. It is only the programmers that associate those concepts with one dimension or another of the array. You can interchange them freely, and store either "rows" or "columns" in any part of the array.
“行”和“列”只是一个解释问题。它们与语言中的任何基本内容都不对应。
(注意:有一些例外;通常是专用数学语言,例如 Matlab。)
"Row" and "column" are just a matter of interpretation. They don't correspond to anything fundamental in the language.
(Note: there are a few exceptions; typically dedicated maths languages, such as Matlab.)
如果您询问行优先顺序与列优先顺序,那么存在细微的低级差异。它归结为多维数据如何存储在内存中。
查看这篇维基百科文章了解更多信息。
If you're asking about row vs. column-major ordering then there is a subtle low-level difference. It boils down to how multi-dimensional data is stored in memory.
Check out this wikipedia article for more info.
正如其他人所回答的,将索引解释为行或列取决于程序员(如何解释具有五个维度的数组?),除了少数语言或语言版本指定了多维数组在内存中的布局方式,以及了解这一点可能有助于通过避免分页和增加 CPU 缓存的命中来提高数组密集型计算的性能。
也就是说,现代优化编译器应该能够找出访问数组的最有效方法,这样程序员就不必担心内存布局。
如果依赖性分析(也数据流分析)确定循环可以展开,并且一次迭代中的表达式不依赖于先前迭代中的表达式,然后可以对表达式进行重新排序,例如通过反转索引。
这与确定循环是否可自动并行化所进行的分析相同。这是在函数式语言中更常见的事情,因为值的不变性使分析更容易。在过程语言中,副作用可能隐藏在任何地方,因此优化仅针对涉及简单表达式的循环进行。
As others have answered, interpreting indexes as rows or columns is up to the programmer (how do you interpret an array with five dimensions?), except that a few languages or language versions specify how multi-dimensional arrays are laid out in memory, and knowing that may help improve the performance of array-intensive computations by avoiding paging and increasing the hits on the CPU caches.
That said, modern optimizing compilers should be able to figure out the most efficient way to access an array so programmers don't have to mind about memory layouts.
If dependence analysis (also Dataflow Analysis)determines that a loop can be unfolded and that the expressions in one iteration don't depend on the ones from previous iterations, then the expressions may be reordered, for example by inverting the indexation.
It is the same kind of analysis that is done to determine if a loop is automatically parallelizable. It is something more frequently done in functional languages because the immutability of values makes the analysis easier. In procedural languages, side-effects may hide anywhere, so the optimizations take place only for loops involving simple expressions.