MrP*_*rik 8 c c++ performance stack allocation
在我的小型性能问题调查期间,我注意到一个有趣的堆栈分配功能,这里是测量时间的模板:
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
int x; //for simple optimization suppression
void foo();
int main()
{
const size_t n = 10000000; //ten millions
auto start = high_resolution_clock::now();
for (size_t i = 0; i < n; i++)
{
foo();
}
auto finish = high_resolution_clock::now();
cout << duration_cast<milliseconds>(finish - start).count() << endl;
}
Run Code Online (Sandbox Code Playgroud)
现在全部是关于foo()实现,在每个实现中将总共分配500000 ints:
分配在一个块中:
void foo()
{
const int size = 500000;
int a1[size];
x = a1[size - 1];
}
Run Code Online (Sandbox Code Playgroud)
结果:7.3秒 ;
分为两个部分:
void foo()
{
const int size = 250000;
int a1[size];
int a2[size];
x = a1[size - 1] + a2[size - 1];
}
Run Code Online (Sandbox Code Playgroud)
结果:3.5秒 ;
分为四个部分:
void foo()
{
const int size = 125000;
int a1[size];
int a2[size];
int a3[size];
int a4[size];
x = a1[size - 1] + a2[size - 1] +
a3[size - 1] + a4[size - 1];
}
Run Code Online (Sandbox Code Playgroud)
结果:1.8秒.
等等......我将它分成16个块,结果时间为0.38秒.
请向我解释一下,为什么以及如何发生这种情况?
我使用了MSVC 2013(v120),发布版本.
UPD:
我的机器是x64平台.我用Win32平台编译它.
当我用x64平台编译它时,它在所有情况下产生大约40ms.
为什么平台选择如此影响?
| 归档时间: |
|
| 查看次数: |
259 次 |
| 最近记录: |