21 - Click to edit Master subtitle style Chapter Twenty One...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Click to edit Master subtitle style Chapter Twenty- One Modern Programming Languages, 2nd ed. Cost Models Chapter Twenty- One Modern Programming Languages, 2nd ed. 11 Chapter Twenty- One Modern Programming Languages, 2nd ed. Which Is Faster? ■ Every experienced programmer has a cost model of the language: a mental model of the relative costs of various operations ■ Not usually a part of a language specification, but very important in practice Chapter Twenty- One Modern Programming Languages, 2nd ed. 22 Y=[1|X] append(X,[1],Y) Chapter Twenty- One Modern Programming Languages, 2nd ed. Outline ■ A cost model for lists ■ A cost model for function calls ■ A cost model for Prolog search ■ A cost model for arrays ■ Spurious cost models Chapter Twenty- One Modern Programming Languages, 2nd ed. 33 Chapter Twenty- One Modern Programming Languages, 2nd ed. The Cons-Cell List ■ Used by ML, Prolog, Lisp, and many other languages ■ We also implemented this in Java Chapter Twenty- One Modern Programming Languages, 2nd ed. 44 ?- A = , | B = .(1,), | C = .(1,.(2,)). A = , B = [1], C = [1, 2]. A : B : 1 C : 1 2 Chapter Twenty- One Modern Programming Languages, 2nd ed. Shared List Structure Chapter Twenty- One Modern Programming Languages, 2nd ed. 55 ?- D = [2,3], | E = [1|D], | E = [F|G]. D = [2, 3], E = [1, 2, 3], F = 1, G = [2, 3]. F : E : 1 D : 2 3 G : Chapter Twenty- One Modern Programming Languages, 2nd ed. How Do We Know? ■ How do we know Prolog shares list structure—how do we know E=[1|D] does not make a copy of term D ? ■ It observably takes a constant amount of time and space ■ This is not part of the formal specification of Prolog, but is part of the cost model Chapter Twenty- One Modern Programming Languages, 2nd ed. 66 Chapter Twenty- One Modern Programming Languages, 2nd ed. Computing Length ■ length(X,Y) can take no shortcut—it must count the length, like this in ML: ■ Takes time proportional to the length of the list Chapter Twenty- One Modern Programming Languages, 2nd ed. 77 fun length nil = 0 | length (head::tail) = 1 + length tail; Chapter Twenty- One Modern Programming Languages, 2nd ed. Appending Lists Chapter Twenty- One Modern Programming Languages, 2nd ed. 88 ?- H = [1,2], | I = [3,4], | append(H,I,J). H = [1, 2], I = [3, 4], J = [1, 2, 3, 4]. H : 1 2 I : 3 4 J : 1 2 ■ append(H,I,J) can also be expensive: it must make a copy of H Chapter Twenty- One Modern Programming Languages, 2nd ed. Appending ■ append must copy the prefix: ■ Takes time proportional to the length of the first list Chapter Twenty- One Modern Programming Languages, 2nd ed. 99 append(,X,X). append([Head|Tail],X,[Head|Suffix]) :- append(Tail,X,Suffix). Chapter Twenty- One Modern Programming Languages, 2nd ed....
View Full Document

This note was uploaded on 09/30/2011 for the course CS 6371 taught by Professor Hamlen during the Spring '11 term at University of Texas at Dallas, Richardson.

Page1 / 55

21 - Click to edit Master subtitle style Chapter Twenty One...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online