Sim*_*mon 1 c++ performance loops
这是我做的一个小测试,结果让我感到惊讶:做两次相同的循环大约是循环一次的两倍.我猜它是因为内存访问?
float* A = new float[1000000];
float* B = new float[1000000];
int h,w;
h = w = 1000;
CString txt;
double time1, time2;
time1 = Timer::instance()->getTime();
for(int j = 0; j < h; j++){
for(int i = 0; i < w; i++){
A[i+j*w] = 1;
B[i+j*w] = 1;
}
}
time2 = Timer::instance()->getTime();
txt.Format(_T("Both in same loop = %f"),time2-time1);
AfxMessageBox(txt);
time1 = Timer::instance()->getTime();
for(int j = 0; j < h; j++){
for(int i = 0; i < w; i++){
A[i+j*w] = 1;
}
}
for(int j = 0; j < h; j++){
for(int i = 0; i < w; i++){
B[i+j*w] = 1;
}
}
time2 = Timer::instance()->getTime();
txt.Format(_T("Different loops = %f"),time2-time1);
AfxMessageBox(txt);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
552 次 |
| 最近记录: |