Lab - Arrays vs. Vectors: an experiment

Overview

There are a variety of goals for this week's lab:

Vectors

In class, we have already seen that using an array for implementing data structures such as a Stack or a Queue has one significant drawback, namely that you must fix the maximum size of the array before beginning.

One possible solution to avoiding this drawback is implemented by a class called Vector which is included in the java.util package. A Vector is essentially an array of Objects with additional code which expands the size of the array in the case where the current capacity has been exhausted.

In creating a Vector, two issues must be specified. First, the initial size of the Vector must be given as a parameter. Secondly, you must specify the rule used in determining how much additional space gets added when the capacity is exhausted. The default rule is that Java will simply double the current capacity at each expansion. Alternatively, you can specify a fixed increment for expansion, such as to expand the capicty by 1000 objects at a time.

An experiment

Our goal is to experiment on three different approaches for accomplishing the same task while comparing the running time. Specifically, the test we will use as a benchmark is to create a new stack, push() the Integers 1, 2, ..., N onto the stack, and then pop()all N items. You will be asked to record the running times for a variety of values of N, and for all three of the following approaches for maintaining the stack.

For recording running times, we considered asking each of you to bring your stop watch. Unfortunately, this is not the most accurate (or convenient) way to record the running time of a task. Fortunately, Java has a command, System.currentTimeMillis(), which allows a program to record the time according to the computer's system clock (in milliseconds). Therefore, our program can time itself by recording the system time both before and after the body of the test, calculating and outputing the elapsed time as the difference between the two recorded times.

Source code

You will not need to write any code for this lab; instead you will simply need to download the source code we provide.

The driver

The driver for this lab is VectorLab. When running the driver, you will need to specify two arguments which are sent to the main routine at runtime. The first argument is the value for N; the second argument is either the number 1, 2 or 3 depending on which ``Approach'' you wish to use in implementing the Stack.

Specifying arguments is something that is done differently depending on the Java development environment you are using, however all such environments should allow you a way to specify arguments. Details on how to provide such arguments are given in the general programming webpage.

Results

Starting with a value of N=20000, we would like you to run this experiement for each of the three implementations. Furthermore, repeat each specific experiment twice to see how consistent the reported running time is. Continue experiements for values 40000, 80000, 160000, and 320000. You may stop the experiments however if you find that the running time for a specific test excedes 30 seconds (times will vary drastically depending on the machine hardware, the other processes running on the machine, and mostly on the specific Java developent environment you are using).

A table for recording your results follows. We will provide you with two copies of this table, so that you may keep the original for yourself and submit a second copy to the TA to receive credit for the lab.

table

Finally, we would like for you to answer the following questions based on your experiments.

  • Of the three methods, is there a clear winner? Explain.

  • When you ran two trials of each experiment for fixed parameters, how likely was it that they produced the same running time? Were there times where the results were drastically different?

  • For each of the three approaches, make an educated guess about how the running time increases relative to the value of N. For example, when N is doubled, does the running time go up by less than a factor of 2? more than a factor of 2? reasonably close to exactly a factor of 2?
  • [printable form]


    Last modified: 29 August 2002