Your code does nothing with the values โโit computes, allowing the compiler to optimize your code to zero. For example, the compiler will probably include this:
void arrr() { int arr[100000]; LARGE_INTEGER start,end; QueryPerformanceCounter(&start); for(int i=0 ; i <100000 ; ++i) { arr[i] = 10 ; } for(int i=0 ; i <100000 ; ++i) { int x = arr[i]; } QueryPerformanceCounter(&end); arr_time += (end.LowPart - start.LowPart); }
In it:
void arrr() { LARGE_INTEGER start,end; QueryPerformanceCounter(&start); QueryPerformanceCounter(&end); arr_time += (end.LowPart - start.LowPart); }
In the static code of the array, the compiler can say that the memory never accesses again, because as soon as the function returns, its stack leaves. In the dynamic case, this is impossible, because he does not know that when memory is freed, its value does not matter. The first cycle should probably remain, but the second cycle is probably completely removed in the dynamic case.
You do not measure what you think you are measuring.
You can try something like this:
void arrr() { int arr[100000]; LARGE_INTEGER start,end; QueryPerformanceCounter(&start); for(int i=0 ; i <100000 ; ++i) { arr[i] = 10 ; } int x = 0; for(int i=0 ; i <100000 ; ++i) { x += arr[i]; } QueryPerformanceCounter(&end); arr_time += (end.LowPart - start.LowPart); cout << "x = " << x << endl; }
There is another difference. In the case of a static array, the compiler knows that no external function (for example, QueryPerformanceCounter ) can depend on the contents of the array or modify it. In the dynamic case, he cannot know this. The position of the QueryPerformanceCounter can be changed relative to the loops. For example, the compiler can move both calls to the QueryPerformanceCounter together, after a loop, in the static case, but not in the dynamic case. (If Microsoft has not used some tricks to prevent this.)