How to Design Programs: An Introduction to Programming and Computing

Info icon This preview shows pages 1–3. Sign up to view the full content.

How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 29 Intermezzo 5: The Cost of Computing and Vectors In section 26.3 we discussed the differences between a structurally recursive program and an equivalent, generative version. The comparison revealed that the generative one is much faster than the structural version. We used both informal arguments, using the number of recursive calls, and measurements, using time expressions (exercises 26.3.1 and 26.3.3 ), to support our conclusion. While timing the application of a program to specific arguments can help us understand a program's behavior in one situation, it is not a fully convincing argument. After all, applying the same program to some other inputs may require a radically different amount of time. In short, timing programs for specific inputs has the same status as testing programs for specific examples. Just as testing may reveal bugs, timing may reveal anomalies concerning the execution behavior for specific inputs. It does not provide a firm foundation for general statements about the behavior of a program. This intermezzo introduces a tool for making general statements about the time that programs take to compute a result. The first subsection motivates the tool and illustrates it with several examples, though on an informal basis. The second one provides a rigorous definition. The last one uses the tool to motivate an additional class of Scheme data and some of its basic operations. 29.1 Concrete Time, Abstract Time Let's study the behavior of how-many , a function that we understand well: (define (how-many a-list) (cond [(empty? a-list) 0] [else (+ (how-many (rest a-list)) 1)])) It consumes a list and computes how many items the list contains. Here is a sample evaluation: (how-many (list 'a 'b 'c)) = (+ (how-many (list 'b 'c)) 1) (1 of 20) [2/5/2008 4:54:28 PM]
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.