Docsity

Measuring Execution Time - Lecture Notes | CS 261, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Chapter 4: Measuring Execution Time 1
Chapter 4: Measuring Execution Time
You would think that measuring the execution time of a program would be easy. Simply
use a stopwatch, start the program, and notice how much time it takes until the program
ends. But this sort of measurement, called a wall-clock time,
is for several reasons not the best characterization of a
computer algorithm. A benchmark describes only the
performance of a program on a specific machine on a given
day. Different processors can have dramatically different
performances. Even working on the same machine, there
may be a selection of many alternative compilers for the
same programming language. For this and other reasons,
computer scientists prefer to measure execution time in a
more abstract fashion.
Algorithmic Analysis
Why do dictionaries list words in alphabetical order? The answer may seem obvious, but
it nevertheless can lead to a better understanding of how to abstractly measure execution
time. Perform the following mind experiment.
Suppose somebody were to ask you to look up the
telephone number for “Chris Smith” in the directory
for a large city. How long would it take? Now,
suppose they asked you to find the name of the
person with number 564-8734. Would you do it? How long do you think it would take?
Is there a way to quantify this intuitive feeling that searching an ordered list is faster than
searching an unordered one? Suppose n represents the number of words in a collection. In
an unordered list you must compare the target word to each list word in turn. This is
called a linear search. If the search is futile; that is, the word is not on the list, you will
end up performing n comparisons. Assume that the amount of time it takes to do a
comparison is a constant. You don’t need to know what the constant is; in fact it really
doesn’t matter what the constant is. What is important is that the total amount of work
you will perform is proportional to n. That is to say, if you were to search a list of 2000
words it would take twice as long as it would to search a list of 1000 words. Now
suppose the search is successful; that is, the word is found on the list. We have no idea
where it might be found, so on average we would expect to search half the list. So the
expectation is still that you will have to perform about n/2 comparisons. Again, this value
is proportional to the length of the list – if you double the length of the list, you would
expect to do twice as much work.
What about searching an ordered list? Searching a dictionary or a phonebook can be
informally described as follows: you divide the list in half after each comparison. That is,
after you compare to the middle word you toss away either the first half or the last half of
the list, and continue searching the remaining, and so on each step. In an earlier chapter
Abby Smith 954-9845
Chris Smith 234-7832
Fred Smith 435-3545
Jaimie Smith 845-2395
Robin Smith 436-9834
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Measuring Execution Time - Lecture Notes | CS 261 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Chapter 4: Measuring Execution Time

You would think that measuring the execution time of a program would be easy. Simply use a stopwatch, start the program, and notice how much time it takes until the program ends. But this sort of measurement, called a wall-clock time, is for several reasons not the best characterization of a computer algorithm. A benchmark describes only the performance of a program on a specific machine on a given day. Different processors can have dramatically different performances. Even working on the same machine, there may be a selection of many alternative compilers for the same programming language. For this and other reasons, computer scientists prefer to measure execution time in a more abstract fashion.

Algorithmic Analysis

Why do dictionaries list words in alphabetical order? The answer may seem obvious, but it nevertheless can lead to a better understanding of how to abstractly measure execution time. Perform the following mind experiment. Suppose somebody were to ask you to look up the telephone number for “Chris Smith” in the directory for a large city. How long would it take? Now, suppose they asked you to find the name of the person with number 564-8734. Would you do it? How long do you think it would take?

Is there a way to quantify this intuitive feeling that searching an ordered list is faster than searching an unordered one? Suppose n represents the number of words in a collection. In an unordered list you must compare the target word to each list word in turn. This is called a linear search. If the search is futile; that is, the word is not on the list, you will end up performing n comparisons. Assume that the amount of time it takes to do a comparison is a constant. You don’t need to know what the constant is; in fact it really doesn’t matter what the constant is. What is important is that the total amount of work you will perform is proportional to n. That is to say, if you were to search a list of 2000 words it would take twice as long as it would to search a list of 1000 words. Now suppose the search is successful; that is, the word is found on the list. We have no idea where it might be found, so on average we would expect to search half the list. So the expectation is still that you will have to perform about n/2 comparisons. Again, this value is proportional to the length of the list – if you double the length of the list, you would expect to do twice as much work.

What about searching an ordered list? Searching a dictionary or a phonebook can be informally described as follows: you divide the list in half after each comparison. That is, after you compare to the middle word you toss away either the first half or the last half of the list, and continue searching the remaining, and so on each step. In an earlier chapter

Abby Smith 954- Chris Smith 234- Fred Smith 435- Jaimie Smith 845- Robin Smith 436-

you learned that this is termed a binary search. If you have been doing the worksheets you have seen binary search already in worksheet 5. Binary search is similar to the way you guess a number if somebody says “I’m thinking of a value between 0 and 100. Can you find my number?” To determine the speed of this algorithm, we have to ask how many times can you divide a collection of n elements in half.

To find out, you need to remember some basic facts about two functions, the exponential and the logarithm. The exponential is the function you get by repeated multiplication. In computer science we almost always use powers of two, and so the exponential sequence is 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, and so on. The logarithm is the inverse of the exponential. It is the number that a base (generally 2) must be raised to in order to find a value. If we want the log (base 2) of 1000, for example, we know that it must be between 9 and 10. This is because 2^9 is 512, and 2^10 is 1024. Since 1000 is between these two values, the log of 1000 must be between 9 and 10. The log is a very slow growing function. The log of one million is less than 20, and the log of one billion is less than 30.

It is the log that provides the answer to our question. Suppose you start with 1000 words. After one comparison you have 500, after the second you have 250, after the third 125, then 63, then 32, then 16, 8, 4, 2 and finally 1. Compare this to the earlier exponential sequence. The values are listed in reverse order, but can never be larger than the corresponding value in the exponential series. The log function is an approximation to the number of times that a group can be divided. We say approximation because the log function returns a fractional value, and we want an integer. But the integer we seek is never larger than 1 plus the ceiling (that is, next larger integer) of the log.

Performing a binary search of an ordered list containing n words you will examine approximately log n words. You don’t need to know the exact amount of time it takes to perform a single comparison, as it doesn’t really matter. Represent this by some unknown quantity c, so that the time it takes to search a list of n words is represented by c * log n. This analysis tells us is the amount of time you expect to spend searching if, for example, you double the size of the list. If you next search an ordered collection of 2n words, you would expect the search to require c * log (2n) steps. But this is c * (log 2 + log n ). The log 2 is just 1, and so this is nothing more than c + c * log n or c * (1 + log n). Intuitively, what this is saying is that if you double the size of the list, you will expect to perform just one more comparison. This is considerably better than in the unordered list,

Logarithms

To the mathematician, a logarithm is envisioned as the inverse of the exponential function, or as a solution to an integral equation. However, to a computer scientist, the log is something very different. The log, base 2, of a value n is approximately equal to the number of times that n can be split in half. The word approximately is used because the log function yields a fractional value, and the exact figure can be as much as one larger than the integer ceiling of the log. But integer differences are normally ignored when discussing asymptotic bounds. Logs occur in a great many situations as the result of dividing a value repeatedly in half.

values, a cubic function (c * r^3 , i.e. gravity) will eventually grow larger than a quadratic function (d * r^2 , i.e. surface tension).

The same idea is used in algorithmic analysis. We say that one function dominates another if as the input gets larger the second will always be larger than the first, regardless of any constants involved. To see how this relates to algorithms consider the function to initialize an identity matrix. If you apply the techniques from the worksheets it is clear the first part is performing O(n 2 ) work, while the second is O(n). So you might think that the algorithm is O(n^2 +n). But instead, the rule is that when summing big- Oh values you throw away everything except the dominating function. Since n^2 dominates n, the algorithm is said to be O(n^2 ).

What functions dominate each other? The table below lists functions in order from most costly to least. The middle column is the common name for the function. The third column can be used to illustrate why the dominating rule makes sense. Assume that an input is size 10^5 , and you can perform 10^6 operations per second. The third column shows the approximate time it would take to perform a task if the algorithm is of the given complexity. So imagine that we had an algorithm had one part that was O(n^2 ) and another that was O(n). The O(n^2 ) part would be taking about 2.8 hours, while the O(n) part would contribute about 0.1 seconds. The smaller value gets overwhelmed by the larger.

In worksheet 9 you will explore several variations on the idea of dominating functions.

Each of the functions commonly found as execution times has a characteristic shape when plotted as a graph. Some of these are shown at right. With experience you should be able to look at a plot and recognize the type of curve it represents.

Function Common name Running time N! Factorial 2 n^ Exponential > century N d^ , d > 3 Polynomial N 3 Cubic 31.7 years

N 2 Quadratic 2.8 hours N sqrt n 31.6 seconds N log n 1.2 seconds N Linear 0.1 second sqrt (n) Root-n 3.2 * 10 -4^ seconds Log n Logarithmic 1.2 * 10-5^ seconds 1 Constant

void makeIdentity (int m[N][N]) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) m[i][j] = 0; for (int i = 0; i < N; i++) m[i][i] = 1; }

10

8

6

4

2

0 0 2 4 6 8 10

linear quadratic cubic root-n log n constant

The Limits of big-Oh

The formal definition of big-Oh states that if f(n) is a function that represents the actual execution time of an algorithm, the algorithm is O(g(n)) if, for all values n larger than a fixed constant n 0 , there exists a constant c, such that f(n) is always bounded by (that is, smaller than or equal to) the quantity c * g(n). Although this formal definition may be of critical importance to theoreticians, an intuitive felling for algorithm execution is of more practical importance.

The big-oh characterization of algorithms is designed to measure the behavior as inputs become ever larger. They may not be useful when n is small. In fact, for small values of n it may be that an O(n^2 ) algorithm, for example, is faster than an O(n log n) algorithm, because the constants involve mask the asymptotic behavior. A real life example of this is matrix multiplication. There are algorithms for matrix multiplication that are known to be faster than the naïve O(n^3 ) algorithm shown in worksheet 8. However, these algorithms are much more complicated, and since the value n (representing the number of rows or columns in a matrix) seldom gets very large, the classic algorithm is in practice faster.

Another limitation of a big-oh characterization is that it does not consider memory usage. There are algorithms that are theoretically fast, but use an excessively large amount of memory. In situations such as this a slower algorithm that uses less memory may be preferable in practice.

Using Big-Oh to Estimate Wall Clock Time

A big-Oh description of an algorithm is a characterization of the change in execution time as the input size changes. If you have actual execution timings (“wall clock time”) for an algorithm with one input size, you can use the big-Oh to estimate the execution time for a different input size. The fundamental equation says that the ratio of the big- Oh’s is equal to the ratio of the execution times. If an algorithm is O(f(n)), and you know that on input n 1 it takes time t 1 , and you want to find the time t 2 it will take to process an input of size n 2 , you create the equation

f(n 1 ) / f(n 2 ) = t 1 / t 2

To illustrate, suppose you want to actually perform the mind experiment given at the beginning of this chapter. You ask a friend to search for the phone number for “Chris Smith” in 8 pages of a phone book. Your friend does this in 10 seconds. From this, you can estimate how long it would take to search a 256 page phone book. Remembering that binary search is O(log n), you set up the following equation:

log(8)/log(256), which is 3 / 8 = 10 / X

Solving for X gives you the answer that your friend should be able to find the number in about 24 seconds. Now you time your friend perform a search for the name attached to a

In worksheet 11 you will use these forms to estimate the running time of various recursive algorithms. An analysis exercise at the end of this chapter asks you to verify the solution of each of these equations.

Best and Worst Case Execution Time

The bubble sort and selection sort algorithms require the same amount of time to execute, regardless of the input. However, for many algorithms the execution time will depend upon the input values, often dramatically so. In these situations it is useful to understand the best situation, termed the best case time , and the worst possibility, termed the worst case time. We can illustrate this type of analysis with yet another sorting algorithm. This algorithm is termed insertion sort. If you have been doing the worksheets you will remember this from worksheet 7.

The insertion sort algorithm is most easily described by considering a point in the middle of execution:

P

already sorted Yet to be considered

Let p represent an index position in the middle of an array. At this point the elements to the left of p (those with index values smaller than p) have already been sorted. Those to the right of p are completely unknown. The immediate task is simply to place the one element that is found at location p so that it, too, will be part of the sorted portion. To do this, the value at location p is compared to its neighbor. If it is smaller, it is swapped with its neighbor. It is then compared with the next element, possibly swapped, and so on.

This process continues until one of two conditions occur. Either a value is found that is smaller, and so the ordering is established, or the value is swapped clear to the start of the array.

Since we are using a while loop the analysis of execution time is not as easy as it was for selection sort.

Question : What type of value will make your loop execute the fewest number of iterations? What type of value will make your loop execute the largest number of times? If there are n elements to the left of p, how many times will the loop iterate in the worst case? Based on your observations, can you say what type of input will make the insertion sort algorithm run most quickly? Can you characterize the running time as big-Oh? What type of input will make the insertion sort algorithm run most slowly? Can you characterize the running time as big-Oh?

We describe this difference in execution times by saying that one value represents the best case execution time, while the other represents the worst case time. In practice we are also interested in a third possibility, the average case time. But averages can be tricky

  • not only can the mathematics be complex, but the meaning is also unclear. Does average mean a random collection of input values? Or does it mean analyzing the sorting time for all permutations of an array and computing their mean?

Most often one is interested in a bound on execution time, so when no further information is provided you should expect that a big-Oh represents the worst case execution time.

Shell Sort

In 1959 a computer scientist named Donald Shell argued that any algorithm that sorted by exchanging values with a neighbor must be O(n^2 ). The argument is as follows. Imagine the input is sorted exactly backwards. The first value must travel all the way to the very end, which will requires n steps.

The next value must travel almost as far, taking n-1 steps. And so on through all the values. The resulting summation is 1 + 2 + … + n which, we have seen earlier, results in O(n 2 ) behavior.

To avoid this inevitable limit, elements must “jump” more than one location in the search for their final destination. Shell proposed a simple modification too insertion sort to accomplish this. The outermost loop in the insertion sort procedure would be surrounded by yet another loop, called the gap loop. Rather than moving elements one by one, the outer loop would, in effect, perform an insertion sort every gap values. Thus, elements could jump gap steps rather than just a single step. For example, assume that we are sorting the following array of ten elements:

0 1 2 3 4 5 6 7 8 9

The gap size can be any decreasing sequence of values, as long as they end with 1. Shell suggested the sequence n/2, n/4, n/8, … 1. Other authors have reported better results with different sequences. However, dividing the gap in half has the advantage of being extremely easy to program.

With the information provided above you should now be able to write the shell sort algorithm (see questions at the end of the chapter). Remember, this is simply an insertion sort with the adjacent element being the value gap elements away:

The analysis of shell sort is subtle, as it depends on the mechanism used in selecting the gaps. However, in practice it is considerably faster than simple insertion sort–despite the fact that shell sort contains three nested loops rather than two. The following shows the result of one experiment sorting random data values of various sizes.

Merge Sort

In this chapter we have introduced several classic sorting algorithms: selection sort, insertion sort and shell sort. While easy to explain, both insertion sort and selection sort are O(n^2 ) worst case. Many sorting algorithms can do better. In this lesson we will explore one of these.

As with many algorithms, the intuition for merge sort is best understood if you start in the middle, and only after having seen the general situation do you examine the start and finish as special cases. For merge sort the key insight is that two already sorted arrays can be very rapidly merged together to form a new collection. All that is necessary is to walk down each of the original lists in order, selecting the smallest element in turn:

When you reach the end of one of the arrays (you cannot, in general, predict which list will end first), you must copy the remainder of the elements from the other.

Based on this description you should now be able to complete the implementation of the merge method. This you will do in worksheet 12. Let n represent the length of the result. At each step of the loop one new value is being added to the result. Hence the merge algorithm is O(n).

Divide and Conquer

The general idea of dividing a problem into two roughly equal problems of the same form is known as the “divide and conquer” heuristic. We have earlier shown that the number of time a collection of size n can be repeatedly split in half is log n. Since log n is a very small value, this leads to many efficient algorithms. We will see a few of these in later lessons.

}

void mergeSortInternal (double data [ ], int low, int high, double temp [ ]) { int i, mid; if (low >= high) return; /* base case / mid = (low + high) / 2; mergeSortInternal(data, low, mid, temp); / first recursive call / mergeSortInternal(data, mid+1, high, temp); / second recursive call / merge(data, low, mid, high, temp); / merge into temp / for (i = low; i <= high; i++) / copy merged values back */ data[i] = temp[i]; }

All that remains is to write the function merge, which you will do in Worksheet 12.

Algorithmic Analysis of Merge Sort

Recall that the analysis of recursive algorithms involves defining the execution time as a recurrence relation. Merge sort is breaking an array of size n into two smaller arrays, and then sorting the smaller arrays. So the equation is roughly

T(N) = 2 * T(N / 2) + c 1 *n + c 2

You will remember that the solution to this relation is approximately n log n. Another way to think about this is the recursive calls on merge sort will be approximately O(log n) levels deep, and at each level it will be doing approximately O(n) operations.

1 2 2 3 3 3 6 7 7 9

1 2 3 6 7

3 7 2 3 9 3 6 1 2 7

(^7 32 )

3 2 7 1

2 3 3 7 9

9 3 6 1 7 2 log n calls deep

n elements wide

An O(n log n) algorithm is a vast improvement over an O(n^2 ) one. To illustrate, you can try executing the merge sort algorithm on arrays of various sizes, and comparing the execution times to that of selection sort. The following table shows some of the values in milliseconds that you would typically discover:

size 5000 6000 7000 8000 9000 10000 selection sort 104 165 230 317 402 500 merge sort 11 12 15 15 19 21

Is it possible to do better? The answer is yes and no. It is possible to show, although we will not do so here, that any algorithm that depends upon a comparison of elements must have at least an O(n log n) worst case. In that sense merge sort is about as good as we can expect. On the other hand, merge sort does have one annoying feature, which is that it uses extra memory. It is possible to discover algorithms that, while not asymptotically faster than merge sort, do not have this disadvantage. We will examine one of these in the next section.

Quick Sort

In this section we introduce quick sort. Quick sort, as the name suggests, is another fast sorting algorithm. Quick sort is recursive, which will give us the opportunity to once again use the recursive analysis techniques introduced earlier in this chapter. Quick sort has differing best and worst case execution times, similar in this regard to insertion sort. Finally, quick sort presents an unusual contrast to the merge sort algorithm.

The quick sort algorithm is in one sense similar to, and in another sense very different from the merge sort algorithm. Both work by breaking an array into two parts, recursively sorting the two pieces, and putting them back together to form the final result. Earlier we labeled this idea divide and conquer.

Where they differ is in the way that the array is broken into pieces, and hence the way that they are put back together. Merge sort devotes very little effort to the breaking apart phase, simply selecting the first half of the array for the first list, and the second half for the second. This means that relatively little can be said about the two sub-arrays, and significant work must be performed to merge them back together.

Quick sort, on the other hand, spends more time on the task of breaking apart. Quick sort selects one element, which is called the pivot. It then divides the array into two sections with the following property: every element in the first half is smaller than or equal to the pivot value, while every

Break

Sort Recursively

Combine

Partition

Sort

themselves larger than the pivot. At each step of the loop the value at the i position is compared to the pivot. If it is smaller or equal, the invariant is preserved, and the i position can be advanced:

Pivot <= Pivot Unknown >= Pivot

i j

Otherwise, the location of the j position is compared to the pivot. If it is larger then the invariant is also preserved, and the j position is decremented. If neither of these two conditions is true the values at the i and j positions can be swapped, since they are both out of order. Swapping restores our invariant condition.

The loop proceeds until the values of i and j meet and pass each other. When this happens we know that all the elements with index values less than i are less than or equal to the pivot. This will be the first section. All those elements with index values larger than or equal to i are larger than or equal to the pivot. This will be the second section. The pivot is swapped back to the top of the first section, and the location of the pivot is returned

<= Pivot Pivot >= Pivot

i

The execution time of quicksort depends upon selecting a good pivot value. Many heuristics have been proposed for this task. These include

  • Selecting the middle element, as we have done
  • Selecting the first element. This avoids the initial swap, but leads to O(n^2 ) performance in the common case where the input is already sorted
  • Selecting a value at random
  • Selecting the median of three randomly chosen values

From the preceding description you should be able to complete the partition algorithm, and so complete the quick sort algorithm. You will do this in worksheet 13.

Study Questions

  1. What is a linear search and how is it different from a binary search?
  2. Can a linear search be performed on an unordered list? Can a binary search?
  1. If you start out with n items and repeatedly divide the collection in half, how many steps will you need before you have just a single element?
  2. Suppose an algorithm is O(n), where n is the input size. If the size of the input is doubled, how will the execution time change?
  3. Suppose an algorithm is O(log n), where n is the input size. If the size of the input is doubled, how will the execution time change?
  4. Suppose an algorithm is O(n^2 ), where n is the input size. If the size of the input is doubled, how will the execution time change?
  5. What does it mean to say that one function dominates another when discussing algorithmic execution times?
  6. Explain in your own words why any sorting algorithm that only exchanges values with a neighbor must be in the worst case O(n^2 ).
  7. Explain in your own words how the shell sort algorithm gets around this limitation.
  8. Give an informal description, in English, of how the merge sort algorithm works.
  9. What is the biggest advantage of merge sort over selection sort or insertion sort? What is the biggest disadvantage of merge sort?
  10. In your own words given an informal explanation of the process of forming a partition.
  11. Using the process of forming a partition described in the previous question, give an informal description of the quick sort algorithm.
  12. Why is the pivot swapped to the start of the array? Why not just leave it where it is? Give an example where this would lead to trouble.
  13. In what ways is quick sort similar to merge sort? In what ways are they different?
  14. What does the quick sort algorithm do if all elements in an array are equal? What is the big-Oh execution time in this case?

include

double getMilliseconds( ) { return 1000.0 * clock( ) / CLOCKS_PER_SEC; }

int main ( ) { double elapsed;

elapsed = getMilliseconds(); … // perform a task elapsed = getMilliseconds() – elapsed; printf(“Elapsed milliseconds = %g\n”, elapsed); }

Using this idea write a program that will determine the execution time for selectionSort for inputs of various sizes. Sort arrays of size n where n ranges from 1000 to 5000 in increments of 500. Initialize the arrays with random values. Print a table of the input sizes and execution times. Then plot the resulting values. Does the shape of the curve look like what you would expect from an n^2 algorithm?

  1. Recall the function given in the previous chapter that you proved computed an^. You can show that this function takes logarithmic number of steps as a function of n. This may not be obvious, since in some steps it only reduces the exponent by subtracting one. First, show that the function takes a logarithmic number of steps if n is a power of n. (Do you see why?). Next argue that every even number works by cutting the argument in half, and so should have this logarithmic performance. Finally, argue that every odd number will subtract one and become an even number, and so the number of times the function is called with an odd number can be no larger than the number of times it is called with an even number.
  2. A sorting algorithm is said to be stable if the relative positions of two equal elements are the same in the final result as in the original vector. Is insertion sort stable? Either give an argument showing that it is, or give an example showing that it is not.
  3. Once you have written the merge algorithm for merge sort, provide invariants for your code and from these produce a proof of correctness.
  4. Is merge sort stable? Explain why or why not.
  5. Assuming your have proved the partition algorithm correct, provide a proof of correctness for the quick sort algorithm.

double exp (double a, int n) { if (n == 0) return 1.0; if (n == 1) return a; if (0 == n%2) return exp(a*a, n/2); else return a * exp(a, n-1); }

  1. Is quick sort stable?

Programming Projects

  1. Experimentally evaluate the running time of Shell Sort versus Insertion sort and Selection Sort. Are your results similar to those reported here?
  2. Experimentally evaluate the running time of Merge sort to that of shell sort and insertion sort.
  3. Rewrite the quick sort algorithm to select a random element in the section being sorted as the pivot. Empirically compare the execution time of the middle element as pivot version of quick sort to this new version. Are there any differences in execution speed?
  4. Experimentally compare the execution time of the partition median finding algorithm to the naïve technique of sorting the input and selecting the middle element. Which one is usually faster?

On the Web

Wikipedia has an excellent discussion of big-Oh notation and its variations.

The Dictionary of Algorithms and Data Structures provided by the National Institute of Standards and Technology (http://www.nist.gov/dads/) has entries on binary and linear search, as well as most other standard algorithms.

The standard C library includes a version of quick sort, termed qsort. However, the interface is clumsy and difficult to use.