哪种方式更适合阵列访问

Which way is better for array access?

本文关键字:阵列 访问 方式更      更新时间:2023-10-16

>我有一个函数,我在其中使用一个常量数组:

void function(int Id){
int array1[4] = {4 constants};
int array2[4] = {4 constants};
   for(int i=0; i<4; i++){
   //accessing the array 1&2 for computation;
   }
}

main()访问void function(int Id)将有近百万次。

我的问题是,在头文件中声明 array1 和 array2 并在 function() 内部访问更好,还是像现在这样动态声明它们更好?

哪种方式会更快(考虑从头文件访问或动态声明)?

编辑:数组仅被访问,不会在function()内部修改。

如果数组不会改变,并且不会在另一个函数中重用,那么最好将它们设为静态。这避免了每次调用函数时在堆栈上构造数组的必要性。

void function(int Id){
    static const int array1[4] = {4 constants};
    static const int array2[4] = {4 constants};
    for(int i=0; i<4; i++){
        //accessing the array 1&2 for computation;
   }
}

编辑以添加 最好避免在数组声明和循环表达式中使用"幻数"4。如果不这样做,很容易更改数组大小而忘记更改循环表达式。这可以通过使数组大小成为常量来完成,或者通过在循环表达式中使用 sizeof() 来完成,如以下堆栈溢出问题所示:如何在 C 中确定数组的大小?

我认为最好的方法是:

void function(int Id){
    static const int array1[4] = {4 constants};
    static const int array2[4] = {4 constants};
   for(int i=0; i<4; i++){
   //accessing the array 1&2 for computation;
   }
}

但最好只做一个小测试,看看哪一个最快。拉克斯万。

我猜没有区别。你可能想写:

**const** int array1[4]

为了更好地向编译器解释你的意思。这可能会给它更多的优化选择。

我尝试了一个测试用例来比较这三个选项 - global, local, local static 对于 4d 矢量的简单矢量内积的大约 2000 万次操作。这是在VS2010 32位发布版本上完成的。结果如下:

DPSUM:600000000 时间:78|DPSUM:600000000 时间:62|DPSUM:600000000 时间:63|DPSUM:600000000 时间:47|DPSUM:600000000 时间:46| DPSUM:600000000 时间:78|DPSUM:600000000 时间:47|DPSUM:600000000 时间:47|DPSUM:600000000 时间:78|DPSUM:600000000 时间:47| DPSUM:600000000 时间:47|DPSUM:600000000 时间:62|DPSUM:600000000 时间:62|DPSUM:600000000 时间:47|DPSUM:600000000 时间:63| DPSUM:600000000 时间:46|DPSUM:600000000 时间:63|DPSUM:600000000 时间:62|DPSUM:600000000 时间:47|DPSUM:600000000 时间:47| DPSUM:600000000 时间:78|DPSUM:600000000 时间:47|DPSUM:600000000 时间:46|DPSUM:600000000 时间:78|DPSUM:600000000 时间:47| DPSUM:600000000 时间:47|DPSUM:600000000 时间:62|DPSUM:600000000 时间:63|DPSUM:600000000 时间:47|DPSUM:600000000 时间:62|

第一列是static const,第二列是local,第三列是global。如果您想在您的平台上尝试,我将发布示例代码。看起来 static locallocal 同样快 - 至少对于这个编译器(可能是由于一些内部优化。

代码如下:

#include <stdio.h>
#include <windows.h>
int ag[] = {1,2,3,4}; int bg[] = {1,2,3,4};
int dp1(){
    static const int a[] = {1,2,3,4}; static const int b[] = {1,2,3,4};
    return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
}
int dp2(){
    int a[] = {1,2,3,4}; int b[] = {1,2,3,4};
    return a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3];
}
int dp3(){
    return ag[0]*bg[0] + ag[1]*bg[1] + ag[2]*bg[2] + ag[3]*bg[3];
}
int main(){
    int numtrials = 10;
    typedef int (*DP)();
    DP dps[] = {dp1, dp2, dp3};
    for (int t = 0; t < numtrials; ++t){
        int dpsum[] = {0,0,0};
        for (int jj =0; jj <3; ++jj){
            DWORD bef, aft;
            bef = GetTickCount();
            for (int ii =0; ii< 20000000; ++ii){
                dpsum[jj] += dps[jj]();
            }
            aft = GetTickCount();
            printf("DPSUM:%d TIME:%d| ", dpsum[jj], aft - bef);
        }
        printf("n");
    }
    getchar();
}