Reputation: 1894
I am a little stuck on this question in my homework: Write three functions in C or C++: one that declares a large array statically, one that declares the same large array on the stack, and one that creates the same large array from the heap. Call each of the subprograms a large number of times {at least 100,000) and output the time required by each. Explain the results.
int main(void)
{
int staticIntArray[ARRAY_SIZE];//array on the stack
int *ptrArray = new int[ARRAY_SIZE]; // pointer on the stack but array on the heap.
double timeItTakes;
clock_t tStart = clock();
fillWithRandomNumbers(staticIntArray, ARRAY_SIZE);
double time = static_cast<double>(clock() - static_cast<double>(tStart)/static_cast<double>(CLOCKS_PER_SEC));
printf ("Array on stack time is %.10f\n", time);
clock_t tStart2 = clock();
fillWithRandomNumbers(ptrArray, ARRAY_SIZE);
double time2 = static_cast<double>(clock() - static_cast<double>(tStart2)/static_cast<double>(CLOCKS_PER_SEC));
printf ("Array on heap time is %.10f\n", time2);
//cout << "Array on the heap time is " << (timeIntStack - time(NULL));
}
void fillWithRandomNumbers(int intArray[], int size)
{
for(int i = 0; i<size; i++)
intArray[i] = rand();
}
The output is:
Array on stack time is 1.9990000000
Array on heap time is 2.9980000000
Press any key to continue . . .
What I understand is that the stack is much smaller allocation of memory that is for local variables and parameters while the heap is a large pool of dynamically allocated memory. Here are my questions... Does using the random class affect the time it takes for the function to execute? Is allocating large arrays on the stack slower because there is less memory available?
I am not trying ask you to do my homework but I just need a little help clarifying the concepts... Any help would be much appreciated...
Upvotes: 3
Views: 2774
Reputation: 101456
First, allow me a slight rant.
The assignment -- presumably in a C++ programming class -- is a bad one. It is diverting your focus to the performance implications of dynamic allocation versus static or automatic allocation, but that is not the main reason you should choose one form of allocation over another. Rather, lifetime and visibility requirements in addition to ownership semantics should be considered long before performance when deciding how to allocate a chunk of memory. Even setting this argument aside, the test is still invalid because the hardware you run the code on, the size of the individual elements in the array and the array itself, the operating system, how the kernel blocks when allocating and the optimization the compiler is permitted to employ are all going to effect the execution speed in any real code you write. But this assignment seems to be suggesting that you should conclude, "see? Dynamic allocation is slower. We should never use it." That reasoning is incorrect and teaches you to employ premature optimization.
OK, end of my rant.
On to your assignment. You are doing two main things wrong.
Upvotes: 6
Reputation: 12321
First you are not doing what the assignment asks for.
Second you should expect the stack allocation to be much faster, since the only thing that is does internally is to move the stack pointer.
Upvotes: 1
Reputation: 36049
You are only allocating your arrays once, at the start of the program, and only writing their contents in the fillWithRandomNumbers loop. The program didn't measure allocation at all; for this to happen, the new
operator should have been within a loop.
Try to follow the assignment here: Write three functions, with each function allocating the array in a different way.
Upvotes: 3