【发布时间】:2015-09-12 06:51:05
【问题描述】:
我正在用 C++ 编写代码,我可以选择运行 1 个 for 循环和 4 个加法操作,或者运行 4 个单独的 for 循环,每个循环都有 1 个加法操作。 (作为旁注,我正在考虑这个问题,因为 4-loops-1-addition 意味着我在我正在编写的程序中分配了 1/4 的内存)
本能地我希望 1-loop-4-additions 更快,我做了一个快速的基准测试来证明这一点。 1-loop-4-addition 的时间大约是 4-loops-1-addition 的一半
我的问题:正在发生什么过程来产生这种差异?
以下是我用于测试的代码 - 我是数学家而不是程序员,所以我很可能会做一些愚蠢的事情。我正在使用 2D 数组,因为这就是我正在编码的内容。
#include <stdio.h>
#include <ctime>
#include <iostream>
using namespace std;
int main(){
int Nx=100;
int Ny=Nx;
double holder=0;
double test[Nx][Ny];
double test1[Nx][Ny];
double test2[Nx][Ny];
double test3[Nx][Ny];
double test4[Nx][Ny];
for(int i=0;i<Nx;i++){
for(int j=0;j<Nx;j++){
test[i][j]=1;
test1[i][j]=1;
test2[i][j]=1;
test3[i][j]=1;
test4[i][j]=1;
}
}
clock_t begin= clock();
for(int i=0;i<Nx;i++){
for(int j=0;j<Ny;j++){
holder=holder + test[i][j];
}
}
for(int i=0;i<Nx;i++){
for(int j=0;j<Ny;j++){
holder=holder + test[i][j];
}
}
for(int i=0;i<Nx;i++){
for(int j=0;j<Ny;j++){
holder=holder + test[i][j];
}
}
for(int i=0;i<Nx;i++){
for(int j=0;j<Ny;j++){
holder=holder + test[i][j];
}
}
clock_t end = clock();
double elapsed = (double) (end-begin)/CLOCKS_PER_SEC;
cout<<"Time to run 1 addition in 4 for loops="<<elapsed<<endl;
begin= clock();
for(int i=0;i<Nx;i++){
for(int j=0;j<Ny;j++){
holder=holder + test1[i][j]+ test2[i][j]+ test3[i][j]+ test4[i][j];
}
}
end = clock();
elapsed = (double) (end-begin)/CLOCKS_PER_SEC;
cout<<"Time to run 4 additions in 1 for loop="<<elapsed<<endl;
}
【问题讨论】:
-
"我正在用 C 编写代码" -- 但我看到的是 C++ 代码。
-
100 次迭代你根本看不到任何差异。
-
最明显的区别是比较的次数。考虑一下
j < Ny在每种情况下总共运行了多少次。 -
嗯,你在分析优化代码吗?每个 C 编译器都知道如何展开循环,手动展开是不必要的。并且每个 C 编译器都可以生成一个汇编列表,看看。
-
@e4c5 它是 100x100 次迭代 - 当我在计算机上运行它时,我不得不将它限制在这个数字上,以避免 seg 错误@HansPassant 正如我所说,我不是程序员,但是当我用 clang++ 编译它时,我确实有
-o标志
标签: c++ performance for-loop