哪种方式更适合阵列访问
Which way is better for array access?
>我有一个函数,我在其中使用一个常量数组:
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 local
和 local
同样快 - 至少对于这个编译器(可能是由于一些内部优化。
代码如下:
#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();
}
- 回旋/修改阵列访问
- 访问特定阵列位置/索引时出现分段错误
- 如何将1D阵列访问为2D阵列
- 分配/访问2d阵列,使得2d子块是连续的
- 将积分类型的数组作为另一个不相关的积分类型的阵列进行访问的安全且符合标准的方法
- 访问多个阵列时 CPU 缓存的作用是什么?
- OpenMP 环路阵列访问中的错误共享
- 访问字符阵列中不可用的内存位置(超出范围值)
- 将std ::向量转换为阵列和当时的p/调用它会导致访问违规例外,在编组期间mscorlib.dll
- C 使用坐标结构的访问阵列元素
- C 私有阵列访问和存储
- 是二维阵列访问/修改时间组
- 阵列访问期间出现分段错误
- 动态2D阵列访问侵犯剩余
- 为什么2D数组访问要比1D阵列访问更快
- 2D阵列访问元素和地址
- 为什么unordered_set不提供阵列访问运算符
- 哪种方式更适合阵列访问
- 为什么这样做?阵列访问不正常
- 2D阵列访问冲突