The question title is quite self-explanatory. I have an run loop that need a dynamic-sized array. But I do know the maximum of that size is going to be, so if needed, I can max it out instead of dynamically-sizing it.
Here's my code, I know that clock_t probably not the best choice for timing in terms of portability, but clock_t provide bad accuracy.
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <ctime>
#define TEST_SIZE 1000000
using namespace std;
int main(int argc, char *argv[])
{
int* arrayPtr = NULL;
int array[TEST_SIZE];
int it = 0;
clock_t begin, end;
begin = clock();
memset(array, 0, sizeof(int) * TEST_SIZE);
end = clock();
cout << "Time to memset: "<< end - begin << endl;
begin = clock();
fill(array, array + TEST_SIZE, 0);
end = clock();
cout << "Time to fill: "<< end - begin << endl;
begin = clock();
for ( it = 0 ; it < TEST_SIZE ; ++ it ) array[it] = 0;
end = clock();
cout << "Time to for: "<< end - begin << endl;
}
Here's my result:
Time to memset: 1590
Time to fill: 2334
Time to for: 2371
Now that I know new & delete does now zero-out the array, is there any way faster than these?
Please help me!