简体   繁体   English

C / C ++如何将3维数组存储在内存中以及遍历数组的最快方法是什么

[英]C/C++ How a 3 dimensional array is stored in memory and what is the fastest way to traverse it

I am trying to understand how a 3 dimensional array is stored in memory and the difference between how std:vector is stored. 我试图了解如何将3维数组存储在内存中以及如何存储std:vector之间的差异。

This is the way I understand that they are stored, and std::vectors, same way, with the difference that they make full use of memory blocks a[0][0][0] a[0][0][1] a[0][0][2]... a[0][1][0] a[0][1][1] ... a[1][0][0] a[1][0][1]... 我以这种方式了解它们的存储方式,以及std :: vectors的相同方式,不同之处在于它们充分利用了内存块a [0] [0] [0] a [0] [0] [1 ] a [0] [0] [2] ... a [0] [1] [0] a [0] [1] [1] ... a [1] [0] [0] a [1 ] [0] [1] ...

My goal is to find which is the most efficient way to traverse and array. 我的目标是找到哪种是遍历和排列最有效的方法。

For example, I have array: 例如,我有数组:

v[1000][500][3];

so how is more efficient to traverse it? 那么遍历的效率如何呢?

 for(i = 0; i < 1000; i++) { for(j = 0; j < 500; j++) { for(k = 0; k < 3; ++k) { //some operation } } } 

or may be it would be more efficient to declare the array as; 或将数组声明为:

v[3][500][1000] v [3] [500] [1000]

and to traverse as 并以

for(i = 0; i < 3; i++) { for(j = 0; j < 500; j++) { for(k = 0; k < 1000; ++k) { //some operation } for(i = 0; i <3; i ++){for(j = 0; j <500; j ++){for(k = 0; k <1000; ++ k){//一些操作}

 } } 

Is there any CL tool to visualize how arrays are stored? 是否有任何CL工具可视化数组的存储方式?

You're right in your representation of arrays in memory values are contiguous. 您是对的,您对数组的表示在内存中是连续的。 So an int v[2][2][2] initialized to 0 would look like: 因此,初始化为0的int v [2] [2] [2]看起来像:

[[[0, 0], [0, 0]], [[0, 0], [0, 0]]]

As far as performance goes you want to access data as close to each other as possible to avoid data cache misses so iterating on the outer most dimension first is a good thing since they are located next to each other. 就性能而言,您希望尽可能接近彼此访问数据,以避免数据高速缓存未命中,因此首先在最外部维度进行迭代是一件好事,因为它们彼此相邻。

Something that might happen though with your first example is the compiler might optimize the inner loop(if right conditions are met) and unroll it so you would save some time there by skipping branching. 尽管在第一个示例中可能发生的事情是编译器可能优化了内部循环(如果满足正确的条件)并展开了它,所以您可以通过跳过分支来节省一些时间。

Since both your example are already iterating in the right way, I would say profile it and see which is faster. 由于您的两个示例都已经以正确的方式进行迭代,因此我想介绍一下它,然后看看哪个更快。


std::vector also store its element contiguous in memory but since it is 1 dimension, locality apply by default(provided you aren't iterating randomly). std :: vector也将其元素连续存储在内存中,但由于它是1维的,因此默认情况下会应用局部性(前提是您不是随机进行迭代)。 The good side of vector is they can grow whereas an array can't(automatically anyway). 向量的好处是它们可以增长,而数组则不能(自动)。

When the memory address is continuous (eg, complied time array a[][][]), the most efficient way to traverse a multidimensional array is use a pointer . 当内存地址是连续的(例如,经过编译的时间数组a [] [] [])时,遍历多维数组的最有效方法是使用a pointer The a[i][j][k] actually is &a[0][0][0]+(i*j*k + j*k + k) . a[i][j][k]实际上是&a[0][0][0]+(i*j*k + j*k + k) Thus, initialize a pointer p to the beginning address, then calls *(p++) 因此,初始化指向起始地址的指针p ,然后调用*(p++)

int main() {
    int a[2][3]={{1,2,3},{4,5,6}};
    int *p = &a[0][0];
    for( int i=0; i<6; ++i ){
        cout<<*(p++)<<endl;
    }
    return 0;
}

To make it visible: 使其可见:

#include <iostream>

    int main()
    {
        int a[][3] = { { 0, 1, 2 }, { 3, 4, 5 } };
        int* p = reinterpret_cast<int*>(a);
        for(unsigned i = 0; i < 6; ++i) {
            std::cout << *(p + i);
        }
        std::cout << std::endl;
        return 0;
    }

Shows a row major order - see: http://en.wikipedia.org/wiki/Row-major_order 显示连续的主要订单-请参阅: http : //en.wikipedia.org/wiki/Row-major_order

Having this, you should iterate per row to utilize the cache. 有了这个,您应该对每行进行迭代以利用缓存。 In higher dimension N you will get similar, where each element represents a block of data with a dimension N-1 在更高的维度N中,您将得到类似的结果,其中每个元素代表一个维度为N-1的数据块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 C++ 中转置多维(n &gt; 2)矩阵的最快方法是什么? - What is the fastest way to transpose a multi dimensional (n > 2) matrix in C++? 用c ++在N维的位数组中存储和访问单个位的最快方法是什么? - What is the fastest way to store and access single bits in an N dimensional array of bits in c++? C++中二维数组排序的解决方法? 以及如何排序是最快的排序方式 - Solution of Two-dimensional array of sort in C++? And how sort be the fastest way to sort 在 C++ 中复制数组最快的可移植方法是什么 - What is the fastest portable way to copy an array in C++ 在 C++ 中获取数组中数字频率的最快方法是什么? - What is the fastest way to get the frequency of numbers in an array in C++? 用c ++将文件读入内存的最快方法? - Fastest Way to Read a File Into Memory in c++? 如何在C ++中为静态n维数组分配内存 - How to allocate memory for a static n-dimensional array in c++ 在 C++ 中转置矩阵的最快方法是什么? - What is the fastest way to transpose a matrix in C++? 在 C++ 中初始化一个数组:最快的方法? - Initializing an Array in C++ : the fastest way? 将大量数据从C ++应用程序传递到SQLServer存储过程的最快方法是什么? - What is the fastest way to pass a large amount of data from a C++ application to SQLServer Stored Procedure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM