为什么这么快就跑完了
Why does this run in no time?
本文关键字:为什么 更新时间:2023-10-16
我在Windows和Linux的不同编译器上对数组、向量、boost::array进行基准测试。我遇到了以下奇怪的事情。
我在Linux 3.7.0.7上安装了gcc 4.7.2,标记:
g++ -O3 -g -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
和这个代码:
const int arrLength = 5;
int a[arrLength];
for (int i = 0; i < arrLength; i++) {
a[i] = i * 5;
}
srand(time(0)); // randomise at run time so it cannot be precomputed by the compiler
int numbers[10];
for (auto &i : numbers)
i = rand();
clock_t c;
c = clock();
for (int i = 0; i < 100000000; i++) {
for (int j = 0; j < arrLength; j++)
a[j] += numbers[j%10];
}
// write it out so the compiler doesn't omit the whole operation if the values in the array are not being used
for (int x : a)
cout << x;
cout << endl;
cout << (float) (clock() - c) << endl;
它实际上在0秒内运行…这是怎么发生的?
我的编译器只计算最终结果。下面是我带注释的源代码:
asm volatile("DEBUG_IN");
for (int i = 0; i < 100000000; i++)
{
for (int j = 0; j < arrLength; j++)
{
a[j] += numbers[j % 10];
}
}
asm volatile("DEBUG_OUT");
调用是g++ -std=c++11 -O3 -S -masm=intel
。
#APP
# 21 "/tmp/x.cpp" 1
DEBUG_IN
# 0 "" 2
#NO_APP
mov ecx, DWORD PTR [esp+60]
imul edi, DWORD PTR [esp+56], 100000000
mov eax, DWORD PTR [esp+64]
mov edx, DWORD PTR [esp+68]
mov DWORD PTR [esp+36], edi
imul edi, ecx, 99999999
lea ecx, [ecx+5+edi]
mov DWORD PTR [esp+40], ecx
imul ecx, eax, 99999999
lea eax, [eax+10+ecx]
mov DWORD PTR [esp+44], eax
imul eax, edx, 99999999
lea eax, [edx+15+eax]
mov edx, DWORD PTR [esp+72]
mov DWORD PTR [esp+48], eax
imul eax, DWORD PTR [esp+72], 99999999
lea eax, [edx+20+eax]
mov DWORD PTR [esp+52], eax
#APP
# 31 "/tmp/x.cpp" 1
DEBUG_OUT
# 0 "" 2
#NO_APP
可以看到,这里只有五个简单的赋值。注意[esp+36]
到[esp+52]
分别指numbers
的元素
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 为什么在全局范围内使用"extern int a"似乎不行?
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 为什么会发生堆损坏
- 为什么使用 "this" 指针调用派生成员函数?
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 为什么比较运算符如此快速
- 为什么 Serial.println(<char[]>);返回随机字符?
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 为什么不;名字在地图上是按顺序排列的吗
- 我的字符计数代码计算错误.为什么
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 为什么 std::unique 不调用 std::sort?
- 既然存在危险,为什么项目要使用-I include开关
- 为什么在运行时没有向我们提供有关分段错误的更多信息?