With the following code snippet the difference of performance between the two ways of variable declaration can be checked:
long double sysTime1;
long double time1;
long double sysTime2;
long double time2;
sysTime1 = GetTickCount();
CString test;
int i;
for (i = 0; i < 1000000; i++)
test.Format("Test%d", i+1);
time1 = GetTickCount() - sysTime1;
sysTime2 = GetTickCount();
for (int i = 0; i < 1000000; i++)
{
CString test;
test.Format("Test%d", i+1);
}
time2 = GetTickCount() - sysTime2;
CString difference;
difference.Format("Difference: %lf ms", time2-time1);
MessageBox(NULL, difference, "Performance Test", MB_OK);
The differences on my windows system are:
Compiled with /Od : ca. 700 ms
Compiled with /O2 : ca. 600 ms
Compiled with /Ox : ca. 550 ms
Conclusion:
In time critical functions the first way of declaration should be used.
Best regards
sja