3

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

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;
   }
}

将有近百万次void function(int Id)main().

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

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

编辑:数组只能被访问,不能在function().

4

4 回答 4

15

If the arrays are not going to change, and are not going to be reused in another function, you might be better making them static. This avoids the necessity of the arrays being constructed on the stack on each call of your 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;
   }
}

edit to add It would be good practice to avoid using the "magic number" 4 in your array declaration and loop expression. If this isn't done it's easy to change the array size and forget to change the loop expression. This can be done either by making the array size a constant, or by using sizeof() in your loop expression as shown in this stack overflow question: How do I determine the size of my array in C?

于 2013-09-12T13:25:20.900 回答
5

I think the best way to do it is:

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;
   }
}

But it would be better to just make a small test to see which one is fastest. Raxvan.

于 2013-09-12T13:24:29.540 回答
2

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

**const** int array1[4]

更好地向编译器解释你的意思。这可能会给它更多的选项来进行优化。

于 2013-09-12T13:23:35.900 回答
0

我尝试了一个比较这三个选项的测试用例 -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();
}
于 2013-09-12T14:09:30.807 回答