再presenting一个二维数组作为一维数组 [英] Representing a 2D array as a 1D array

查看:135
本文介绍了再presenting一个二维数组作为一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能显示的文件:

  <一href=\"http://stackoverflow.com/questions/732684/implementing-a-matrix-which-is-more-efficient-using-an-array-of-arrays-2d-or\">Implementing一个矩阵,它是更有效的 - 使用阵列(2D)或一维数组的数组的

  <一href=\"http://stackoverflow.com/questions/1242705/performance-of-2-dimensional-array-vs-1-dimensional-array\">Performance 2维阵列对1维阵列


我一直在寻找我的好友的分子动力学code前些天基地之一,他已经重新presented一些2D数据作为一维数组。因此,而不是使用两个指标他只跟踪一个,但有点数学做是为了弄清楚它会是什么位置,如果它是2D的。因此,在这种二维阵列的情况下:

  two_D = [0,1,2]
         [3,4,5]

这将重新psented为$ P $:

  one_D = [0,1,2,3,4,5]

如果他需要知道什么是二维数组,他会做一些简单的代数和得到4的位置(1,1)。

有没有使用一维数组,而不是一个二维数组获得任何性能提升。在阵列中的数据可以在计算过程中被调用数百万次。<​​/ P>

我希望数据结构的解释是明确的...如果不是让我知道,我会试着更好的解释。

感谢您:)

修改
语言是C


解决方案

在看看<一个href=\"http://stackoverflow.com/questions/1242705/performance-of-2-dimensional-array-vs-1-dimensional-array\">http://stackoverflow.com/questions/1242705/performance-of-2-dimensional-array-vs-1-dimensional-array

Possible Duplicates:
Implementing a matrix, which is more efficient - using an Array of Arrays (2D) or a 1D array?
Performance of 2-dimensional array vs 1-dimensional array

I was looking at one of my buddy's molecular dynamics code bases the other day and he had represented some 2D data as a 1D array. So rather than having to use two indexes he only has to keep track of one but a little math is done to figure out what position it would be in if it were 2D. So in the case of this 2D array:

two_D = [[0, 1, 2],
         [3, 4, 5]]

It would be represented as:

one_D = [0, 1, 2, 3, 4, 5]

If he needed to know what was in position (1,1) of the 2D array he would do some simple algebra and get 4.

Is there any performance boost gained by using a 1D array rather than a 2D array. The data in the arrays can be called millions of times during the computation.

I hope the explanation of the data structure is clear...if not let me know and I'll try to explain it better.

Thank you :)

EDIT The language is C

解决方案

Take a look at http://stackoverflow.com/questions/1242705/performance-of-2-dimensional-array-vs-1-dimensional-array

这篇关于再presenting一个二维数组作为一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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