Write three functions in C or C one that declares a large ar

Write three functions in C or C++: one that declares a large array statically, one that creates the same large array on the stack, and one that creates the same large array on the heap. Call each subprogram a large number of times (e.g., 10,000) and output the time required by each.

Solution

Code in C++ :

#include <iostream>
#include <ctime>
using namespace std;

//functions declaration
void staticArray();
void onHeap(int n);
void onStack(int n);

int main(){
//declare variables for start and end time
unsigned int start ,stop;

//record start time
start = clock();
//call method staticArray() 10000 times
for(int i=0;i<10000;i++)
staticArray();
//record end time
stop = clock();
//print total time taken
cout<<\"Time taken by static method: \"<<(stop - start)<<\" ms\"<<endl;
  
//record start time
start = clock();
//call method onStack() 10000 times
for(int i=0;i<10000;i++)
onStack(100000);
//record end time
stop = clock();
//print total time taken
cout<<\"Time taken by onStack method: \"<<(stop - start)<<\" ms\"<<endl;

//record start time
start = clock();
//call method onHeap() 10000 times
for(int i=0;i<10000;i++)
onHeap(100000);
//record end time
stop = clock();
//print total time taken
cout<<\"Time taken by onHeap method: \"<<(stop - start)<<\" ms\"<<endl;

}

void staticArray(){
//create static array of size 100000
static int array[100000];
}

void onHeap(int n){
//create dynamic array of size 100000
int *array = new int[n];
}
void onStack(int n){
//create local array of size 100000
int array[n];
}

Sample Output :

Time taken by static method: 66 ms                                                                                                                                                                                                              

Time taken by onStack method: 347 ms                                                                                                                                                                                                            

Time taken by onHeap method: 43298 ms                                                                                                                                                                                                           

sh-4.3$ ^C                                                                                                                                                                                                                                      

Write three functions in C or C++: one that declares a large array statically, one that creates the same large array on the stack, and one that creates the same
Write three functions in C or C++: one that declares a large array statically, one that creates the same large array on the stack, and one that creates the same

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site