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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started