Visual C ++算术,`const`比常规变量慢

时间:2015-11-13 16:56:18

标签: performance visual-studio visual-c++ visual-studio-2015 const

所以我搞乱了Visual C ++ 2015,我注意到Visual C ++似乎编译常量和访问它们的方式存在问题。

请考虑以下事项:

yourList.add()

顶部部分比底部部分运行更慢(增加约75%的额外时间),唯一的区别是#include "stdafx.h" #include <stdio.h> #include <Windows.h> #define ITERATIONS 500000 #define GET_START_TIME QueryPerformanceCounter(&StartingTime); #define GET_END_TIME QueryPerformanceCounter(&EndingTime); #define CALC_DIFF_TIME ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart; ElapsedMicroseconds.QuadPart *= 1000000; ElapsedMicroseconds.QuadPart /= Frequency.QuadPart; int main() { short results[ITERATIONS]; const int n = 5; int m = 5; LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds; LARGE_INTEGER Frequency; QueryPerformanceFrequency(&Frequency); // This loop seems to take about 1400 us on my computer. printf("Beginning loop over %i iterations with n constant.\n", ITERATIONS); GET_START_TIME; for (int i = 0; i < ITERATIONS; i++) { int statement = i % 10; if (statement == 0) results[i] = n * 0; else if (statement == 4) results[i] = n * 4; else if (statement == 2) results[i] = n * 2; else if (statement == 5) results[i] = n * 5; else if (statement == 7) results[i] = n * 7; else if (statement == 6) results[i] = n * 6; else if (statement == 1) results[i] = n * 1; else if (statement == 3) results[i] = n * 3; else if (statement == 9) results[i] = n * 9; else if (statement == 8) results[i] = n * 8; } GET_END_TIME; CALC_DIFF_TIME; printf("Finished in %lld us.\n", ElapsedMicroseconds.QuadPart); // This one takes about 800 us on my computer. printf("Beginning loop over %i iterations with m variable.\n", ITERATIONS); GET_START_TIME; for (int i = 0; i < ITERATIONS; i++) { int statement = i % 10; if (statement == 0) results[i] = m * 0; else if (statement == 4) results[i] = m * 4; else if (statement == 2) results[i] = m * 2; else if (statement == 5) results[i] = m * 5; else if (statement == 7) results[i] = m * 7; else if (statement == 6) results[i] = m * 6; else if (statement == 1) results[i] = m * 1; else if (statement == 3) results[i] = m * 3; else if (statement == 9) results[i] = m * 9; else if (statement == 8) results[i] = m * 8; } GET_END_TIME; CALC_DIFF_TIME; printf("Finished in %lld us.\n", ElapsedMicroseconds.QuadPart); getchar(); return 0; } n而不是常规const int

我的问题:这是设计,还是Visual C ++中的缺陷/错误?这是否与我将结果分配给int

的事实有关

经过进一步调试后,似乎如果我将short数组更改为short数组,则常规变量的速度从~800us增加到1400us,int

另外一个问题:为什么将常规变量算术结果的更长分配给const数组,而不是int数组,为什么呢? short花费相同的时间吗?

附加说明:进一步的故障排除显示这只发生在发布模式,而不是在调试模式下。

我已为所有感兴趣的人提供了const int个文件on Gist

0 个答案:

没有答案