This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: CS 134 Tutorial 5: Exact Efficiency and Stacks CS 134 Tutors Winter 2008 1 Efficiency 1.1 Overview When we analyze code for efficiency, we are trying to gauge either the runtime or the maximum space usage of the code. For this tutorial, we only consider runtime. There are many obstacles to calculating runtime. Different machines run with different hard- ware, different software and other varying environmental factors that affect their speeds. For this tutorial, we will limit ourselves to counting particular pre-specified lines in some given code. Later in the course we will develop more formal tools to deal with these problems with deter- mining efficiency. 1.2 How do we quantify runtime? Runtime is not a number. It cannot be represented in milliseconds, cpu cycles, gate flops, or number of lines executed. Runtime is a function of some input parameter. For example, the input parameter might be n = a.length , the length of an array. In this tutorial, we will use f ( n ) to denote the number of times a particular line of code will run on input of size n. 1.3 Best Case vs. Worst Case Though we have chosen some parameter, n , that affects the runtime of the method, this does not mean that for each value of n there is only one value of f ( n ). There may be other parameters that affect the runtime of the method. We can say that f ( n ) is the range of all runtimes the method can have at parameter n . From this family of functions, we can generate two functions in particular: f B ( n ) and f W ( n ) for the best and worst runtimes. f B ( n ) = min f ( n ) f W ( n ) = max f ( n ) 1 f(n) n fw(n) Runtime fb(n) Figure 1: Illustration of runtimes – as a family of functions along with best and worst runtimes. 2 Examples For example: Let n = a.length . For the numPositive() method, find the best and worst case runtimes for the commands listed in the table below....
View Full Document
- Fall '07
- Subroutine, public int, Return statement, The Return, Void type