问题描述
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 :)
EDITThe language is C
Take a look at http://stackoverflow.com/questions/1242705/performance-of-2-dimensional-array-vs-1-dimensional-array
这篇关于再presenting一个二维数组作为一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!