Laboratory Exercise for The Tao of Computing

Run-Time Experiments

Summary: Although computers are often considered to work very quickly, some algorithms do indeed proceed rapidly while others take much longer. This laboratory exercise provides an intuitive framework for the cvonsideration of algorithm effectiveness, including the amount of time and memory required for an algorithm. When algorithms are applied to successively larger data sets, some algorithms may scale up nicely while others require more time than may be feasible. Also, when several algorithms are available to solve a problem, it is natural to wonder if one solution were better than another. Altogether, we might use many criteria to evaluate such solutions, including: For this laboratory exercise, we focus on algorithm execution time.

Algorithm Execution Time

In determining algorithm execution time, we may proceed in several ways: Each of these approaches has advantages, but each also has drawbacks. Execution times on a specific machine normally depend upon details of the machine and on the specific data used. Timings may vary from data set to data set and from machine to machine, so experiments from one machine and one data set may not be very helpful in general.

The analysis of instructions may take into account the nature of the data -- for example, one might consider what happens in a worst case. Also, such analysis commonly is based on the size of the data being processed -- the number of items or how large or small the data are. This is sometimes called a microanalysis of program execution. Once again, however, the specific instructions may vary from machine to machine, and detailed conclusions from one machine may not apply to another.

A high-level analysis may identify types of activities performed, without considering exact timings of instructions. This is sometimes called a macroanalysis of program execution. This can give a helpful overall assessment of an algorithm, based on the size of the data. However, such an analysis cannot show fine variations among algorithms or machines.

For many purposes, it turns out than a high-level analysis provides adequate information to compare algorithms. For the most part, we follow that approach here.

Linear and Binary Searches

In class, we have discussed both the linear search and the binary search.

  1. Write a paragraph or two description of how a linear search works.

  2. Write a paragraph or two description of how a binary search works.

In class, we have discussed how efficient each of these algorithms might be. Now we take a more concrete, experimental approach.

Experiments Regarding Search

We will search for random items in a sequence of even integers:

0, 2, 4, 6, ..., maximum

Techically, the structure holding such a collection of data is called an array. With the array data just described, a search should be successful if we are looking for an even, nonnegative integer that is not too large. The search should fail if the desired integer is negative, odd, or too large. In our experiment, we will pick 10 integers at random as candidates for the search.

The program searchTest performs both a linear search and a binary search for randomly-selected items, and records the time required for this work. Due to the speed of the computer and the limitations of the clocking mechanism available, we repeat each experiment 60,000 times. This means that times are magnified, so we can easily see differences.

The mechanics of running these search experiments are as follows:

In this part of the lab, you are to gather experimental data regarding the search times for various size arrays for both the linear and binary searches. Since the searchTest program performs 10 trials, you will need to combine the results in some way. For example, you might want to average the results or you might choose a median value.

  1. Run searchTest for a variety of array sizes between 1000 and 50,000, recording your results in each case.

  2. Plot the combined values (averages or medians) of your various trials carefully on graph paper. The horizontal axis on the graph paper should indicate the size of the array, and the vertial axis should indicate time. You should conduct sufficient experiments, so that a fairly consistent pattern emerges.

  3. Draw a reasonable graph through your data points to suggest the overall approximate relationship between the size of the array and the time required for a typical search.

  4. Describe (in words) the nature of the graphs you have observed.

Other Experiments for Searching

Your experimental results, of course, relied on a particular program running on a specific machine. Actual numbers are likely to vary from one computer and program to another. However, we still would anticipate the same general patterns -- even if the numbers differ.

The following table gives experimental measurements for the average time required for a linear search for several search trials on another machine with another program.

Array Average Time Average Time If
Size If Value Found Value Not Found
1000 620 1248
2000 1260 2490
4000 2540 4960
  1. Estimate the time for an average linear search of arrays of size 1500, 3000, 8000, and 16000. Briefly justify your answers.

Continuing this experiment, the following table gives experimental measurements for the average time required for a binary search for several search trials.

Array Average Time Average Time If
Size If Value Found Value Not Found
1000 33 33
2000 37 37
4000 41 41

  1. Estimate the time for an average binary search of arrays of size 1500, 3000, 8000, and 16000. Briefly justify your answers.

Work To Be Turned In



This laboratory exercise coordinates with Chapter 6 of Walker, Henry M., The Tao of Computing: A Down-to-earth Approach to Computer Fluency, Jones and Bartlett, March 2004.
created December 31, 2003
last revised February 18, 2003

Valid HTML 4.01! Valid CSS!