Question: Normally we believe static arrays are most efficient ones, however it is very hard to observe but we could try. Write C++ or Java code
- Normally we believe static arrays are most efficient ones, however it is very hard to observe but we could try. Write C++ or Java code to check performance using static arrays as well as stack/heap arrays. The following is a sample C++ program that you may use (you may modify, or rewrite into Java code etc). Observe the results (may or may not convince you.) For submission, include your source code as well as output in plain text.
#include
#include "time.h"
using namespace std;
const int MAXLEN = 10000; //size of array
const int MAXITER = 100000; //# of iterations
void staticArray () {
static double data1[MAXLEN];
for (int i=0; i return; } void heapArray() { double *data2 = new double[MAXLEN]; //Java syntax differs for (int i=0; i delete []data2; //Java will collect garbage automatically } int main() { time_t start, end; double static_time, heap_time; //test static arrays time(&start); for (int iter=0; iter time(&end); static_time = double(end - start); cout << "Static array time is : " << static_time << "sec " << endl; //test heap arrays time(&start); for (int iter=0; iter time(&end); heap_time = double(end - start); cout << "Static array time is : " << heap_time << "sec " << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
