A previous post leaves the answer to this question as unresolved. Yet, my intuition tells me that larger allocations will take longer because it will take longer for underlying allocation strategies to find larger contiguous blocks. The following C++ program, which outputs this:
Total time for size 1048576: 5.50781
Total time for size 2097152: 6.08594
Total time for size 4194304: 6.07031
Total time for size 1048576: 5.48438
Total time for size 2097152: 6.05469
Total time for size 4194304: 6.07031
confirms that larger allocations take longer. Can anyone confirm the outcome of this experiment? Is the intuition I stated above correct?
#include <ctime>
#include <iostream>
int main()
{
// Initialize parameters
int size = 1024*1024;
int sizes[3] = {size, 2*size, 4*size};
int nIter = 2000;
// Repeat the entire experiment twice
for (int count=0; count<2; count++)
{
// Loop though all allocation sizes
for (int i=0; i<sizeof(sizes)/sizeof(int); i++)
{
// Start timer
const clock_t begin_time = clock();
// Total of nIter iterations
for (int j=0; j<nIter; j++)
{
// Allocate array
char *array = new char[sizes[i]];
// Loop through part of the array
for (int k=0; k<size; k++)
array[k] = 1;
// Free array
delete [] array;
}
// Report total time
const float total_time = float(clock()-begin_time)/CLOCKS_PER_SEC;
std::cout << "Total time for size " << sizes[i] << ": " << total_time << std::endl;
}
}
}