Fastest way to copy several 2-dimensional arrays into one 1-dimensional array (in C)

 ̄綄美尐妖づ 提交于 2019-12-25 12:12:06

问题


I want to copy several 2-dimensional subarrays of 3-dimensional arrays (e.g. array1[n][rows][cols], ..., array4[n][rows][cols]), which are dynamically allocated (but with fixed length), into a 1-dimensional array (e.g. array[4*rows*cols]), which is statically allocated, in C. As there will be many rows and columns (e.g. 10000 rows and 500 columns), I was wondering which of the following three possibilities will be the fastest:

    for(i=0;i<rows;i++){
      for(j=0;j<cols;j++){
        array[i*cols+j]=array1[2][i][j];
      }
    }
    ...
    for(i=0;i<rows;i++){
      for(j=0;j<cols;j++){
        array[3*rows*cols+i*cols+j]=array4[2][i][j];
        }
    }

or

    for(i=0;i<rows;i++){
      for(j=0;j<cols;j++){
        array[i*cols+j]=array1[2][i][j];
      }
      ...
      for(j=0;j<cols;j++){
        array[3*rows*cols+i*cols+j]=array4[2][i][j];
      }
    }

or

    for(i=0;i<rows;i++){
      for(j=0;j<cols;j++){
        array[i*cols+j]=array1[2][i][j];
        ...
        array[3*rows*cols+i*cols+j]=array4[2][i][j];
      }
    }

Or is there even a faster way of performing this task in C (not C++)?

Thanks a lot! (I thought there should be a similar question already, but unfortunately did not find exactly what I was looking for. So, I am sorry if I missed such a question.)

Edit: The reason for wanting to do this is the following: The information stored in those 2-dimensional (sub-)arrays has to be communicated via MPI. So, clients will do the above and the master (kind of) the other way round (i.e. 1-dimensional -> 2-dimensional). So, is there even a better way to do this overall?


回答1:


Assuming linear arrays and the same dimensions (which would invalidate my answer):

This

for(i=0;i<rows;i++) {
  for(j=0;j<cols;j++) {
    array[i*cols+j]=array1[2][i][j];
  }
}

could be replace by:

memcpy(array, array1[2], sizeof(array1[2]));

Accordingly this one:

for(i=0;i<rows;i++) {
  for(j=0;j<cols;j++) {
    array[3*rows*cols+i*cols+j]=array4[2][i][j];
  }
}

is going to be:

memcpy(array + 3*cols*rows, array4[2], sizeof(array4[2]));



回答2:


C specifies arrays are stored in row-major order, so each i wrapping every j (and each j wrapping every k, and so on) is going to be faster than the alternative due to "locality of reference."

However, as mentioned in comments, memcpy() will copy the entire space linearly, ignoring the ordering. So, it's almost certainly no worse and it will be far easier to keep straight, with only one call. Just be sure to use sizeof() to get the size of the array as stored, since the actual storage may be larger than just rows*cols.

The only case where memcpy() might be worse is if your array elements are pointers to something else. At that point, you would only be copying the map to the real data, which may not be what you want.



来源:https://stackoverflow.com/questions/22541623/fastest-way-to-copy-several-2-dimensional-arrays-into-one-1-dimensional-array-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!