I was trying to understand performance of global static variable and came across a very weird scenario. The code below takes about 525ms average.
static unsigned long long s_Data = 1;
int main()
{
unsigned long long x = 0;
for (int i = 0; i < 1'000'000'000; i++)
{
x += i + s_Data;
}
return 0;
}
and this code below takes 1050ms average.
static unsigned long long s_Data = 1;
int main()
{
unsigned long long x = 0;
for (int i = 0; i < 1'000'000'000; i++)
{
x += i;
}
return 0;
}
I am aware that accessing static variables are fast, and writing to them is slow based on my other tests but I am not sure what piece of information I am missing out in the above scenario. Note: compiler optimizations were turned off and MSVC compiler was used to perform the tests.