in my current project i need to operate on large data array.
so i do a stupid test to check which one will be better , but while trying below code i found dynamic arrays are much slower than static array why so ? or am i doing something wrong ?
here is code (i remove vector (perform equal to dynamic) and boost array (equal to static) from here)
result : static 8 , dynamic 7493
#include<iostream>
#include<vector>
using namespace std;
using namespace boost;
double arr_time;
double darr_time;
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);
}
void darr()
{
int *arr = new int[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);
darr_time += (end.LowPart - start.LowPart);
delete[] arr;
}
int main(int argc, char** argv)
{
for(int i= 0 ; i <100 ; ++i)
{
arrr();
darr();
}
cout<<"\n--------------------\n";
cout<<arr_time<<endl;
cout<<darr_time<<endl;
return 0;
}
As David said above, it is likely you’re not actually measuring what you think you are due to optimization. Here is your code with some changes to make sure nothing being timed is optimized out.
With this code, in a release build with Visual Studio 2008 and 2010, the times are almost identical as is the assembly code generated by each function. The dynamic time, for me, is always slightly less than the static time, but by such a tiny amount I’d say they are equivalent.