Section 5 - vectors vs. Arrays: The Listing

1B.5.1 The Listing

Here is the entire program whose results we discussed in the last section. Try it out on your system. You may find very different results - maybe even results that contradict my findings. Please report it in the forums because this is a fluid subject matter and I learn from your reports just as much as you do from mine.

// Analysis of arrays vs. vectors using various access techniques
// CS 2C, Foothill College, Michael Loceff, creator

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

#include <vector>

// for timing our algorithms
#include <time.h>
clock_t startClock();
double computeElapsedTime(clock_t startTime);
void displaySeconds(string message, double timeInSecs);

// --------------- main ---------------
#define ARRAY_SIZE 200000

int main()
{
   int k;
   double avg;
   int arrayOfInts[ARRAY_SIZE];
   vector<int> vectorOfInts(ARRAY_SIZE);
   vector<int> vectorOfInts2;
   clock_t startTime; 
   double elapsedTime;

   startTime = startClock();   // START TIME START TIME START TIME ----------
   for (k = 0; k < ARRAY_SIZE; k++)
      arrayOfInts[k] = rand()%100;

   // compute the average using array
   for (k = 0, avg = 0.; k < ARRAY_SIZE; k++)
      avg += arrayOfInts[k];
   avg/=ARRAY_SIZE;
   cout << avg << endl;

   elapsedTime = computeElapsedTime(startTime); // STOP TIME STOP TIME STOP TIME -----
   displaySeconds("Time for simple array: ", elapsedTime);

   startTime = startClock();   // START TIME START TIME START TIME ----------

   for (k = 0; k < ARRAY_SIZE; k++)
      vectorOfInts[k] = rand()%100;

   // compute the average using array
   for (k = 0, avg = 0.; k < ARRAY_SIZE; k++)
      avg += vectorOfInts[k];
   avg/=ARRAY_SIZE;
   cout << avg << endl;

   elapsedTime = computeElapsedTime(startTime); // STOP TIME STOP TIME STOP TIME -----
   displaySeconds("Vector using []: ", elapsedTime);

   startTime = startClock();   // START TIME START TIME START TIME ----------
   for (k = 0; k < ARRAY_SIZE; k++)
      vectorOfInts[k] = rand()%100;

   // compute the average using array
   for (k = 0, avg = 0.; k < ARRAY_SIZE; k++)
      avg += vectorOfInts.at(k);
   avg/=ARRAY_SIZE;
   cout << avg << endl;

   elapsedTime = computeElapsedTime(startTime); // STOP TIME STOP TIME STOP TIME -----
   displaySeconds("Vector using at(): ", elapsedTime);

   startTime = startClock();   // START TIME START TIME START TIME ----------

   for (k = 0; k < ARRAY_SIZE; k++)
      vectorOfInts2.push_back(rand()%100);

   // compute the average using array
   for (k = 0, avg = 0.; k < ARRAY_SIZE; k++)
      avg += vectorOfInts2[k];
   avg/=ARRAY_SIZE;
   cout << avg << endl;

   elapsedTime = computeElapsedTime(startTime); // STOP TIME STOP TIME STOP TIME -----
   displaySeconds("Vector using push_back():" , elapsedTime);

   return 0;
}

// for timing our algorithms
clock_t startClock()
{
   return clock();
}

double computeElapsedTime(clock_t startTime)
{
   clock_t stopTime = clock();
   return (double)(stopTime - startTime)/(double)CLOCKS_PER_SEC;
}

void displaySeconds(string message, double timeInSecs)
{
   cout << "Elapsed time for " << message 
      << timeInSecs  << " seconds." << endl;
}