I have been using vector in c++ lately, but I needed to speed up my code a bit so someone recommended I use regular arrays. My question is 2 part -
1) I thought you couldn't do this!!?? For some reason it is working with no problem?
int a=2;
int test[a];
2) I used a timer (from the VUL library of VXL) to time the following two functions. The only difference is one of the arrays is allocated using a global int, and the other is allocated using a #define. It actually turns out the the global int is twice as fast!!?? I thought they should be about the same (if the int even worked at all! (see question 1))
FunctionWithArray time: 17 ms
FunctionWithDynamicArray time: 8 ms
#include <iostream>
using namespace std;
int DynamicSize = 2000000;
#define ArraySize 2000000
void FunctionWithDynamicArray();
void FunctionWithArray();
int main()
{
FunctionWithDynamicArray();
FunctionWithArray();
return 0;
}
void FunctionWithDynamicArray()
{
int test[DynamicSize];
for(int i = 0; i < DynamicSize; i ++)
{
test[i] = i;
}
}
void FunctionWithArray()
{
int test[ArraySize];
for(int i = 0; i < ArraySize; i ++)
{
test[i] = i;
}
}
Any thoughts on either of these?
Thanks,
Dave