So, I was messing around with Visual C ++ 2015, and I noticed that there is a problem with how Visual C ++ seems to compile constants and their access.
Consider the following:
#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);
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);
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;
}
The upper part is slower (about 75% of the extra time) than the lower section, and the only difference is that it nis const intinstead of the usual one int.
My questions are: is it design or bug / error in Visual C ++? Is this because I am assigning a result short?
After further debugging, it seems that if I change the array shortto an array int, then the speed for the regular variable will increase from ~ 800us to 1400us using the value const.
, : , int, short, const int ?
: , Release, .
.asm Gist , .