SlideShare a Scribd company logo
Class 18:
    Measuring
    Cost



                                                cs1120 Fall 2011
                                                David Evans
Colossus Rebuilt, Bletchley Park, Summer 2004   3 October 2011
Plan
How Computer Scientists Measure Cost
Asymptotic Operators


         Assistant Coaches’ Review Sessions for Exam 1:
             Tuesday (tomorrow), 6:30pm, Rice 442
                 Wednesday, 7:30pm, Rice 442




                                                          2
(define (fibo-rec n)                   (define (fibo-loop n)
    (if (= n 0) 0                          (cdr
        (if (= n 1) 1                       (loop 1 (cons 0 1) (lambda (i) (< i n)) inc
            (+ (fibo-rec (- n 1))                 (lambda (i v)
               (fibo-rec (- n 2))))))               (cons (cdr v) (+ (car v) (cdr v)))))))

> (time (fibo-rec 2))                         > (time (fibo-loop 2))
cpu time: 0 real time: 0 gc time: 0           cpu time: 0 real time: 0 gc time: 0
1                                             1
> (time (fibo-rec 5))                         > (time (fibo-loop 5))
cpu time: 0 real time: 0 gc time: 0           cpu time: 0 real time: 0 gc time: 0
5                                             5
> (time (fibo-rec 20))                        > (time (fibo-loop 20))
cpu time: 0 real time: 2 gc time: 0           cpu time: 0 real time: 0 gc time: 0
6765                                          6765
> (time (fibo-rec 30))                        > (time (fibo-loop 30))
cpu time: 359 real time: 370 gc time: 0       cpu time: 0 real time: 0 gc time: 0
832040                                        832040
> (time (fibo-rec 60))                        > (time (fibo-loop 60))
still waiting since Friday…                   cpu time: 0 real time: 0 gc time: 0
                                              1548008755920
(define (fibo-rec n)                 (define (fibo-loop n)
  (if (= n 0) 0                        (cdr
      (if (= n 1) 1                     (loop 1 (cons 0 1)
          (+ (fibo-rec (- n 1))               (lambda (i) (< i n)) inc
             (fibo-rec (- n 2))))))           (lambda (i v)
                                                (cons (cdr v) (+ (car v) (cdr v)))))))

Number of “expensive” calls:                 Number of “expensive” calls:




where n is the value of the input,
and is the “golden ratio”:
How Computer Scientists
                    Measure Cost
 Abstract: hide all the details that will change
  when you get your next laptop to capture the
  fundamental cost of the procedure
    Cost: number of steps for a Turing Machine to
        execute the procedure
 Order of Growth: what matters is how the cost
  scales with the size of the input
    Size of input: how many TM squares needed to
        represent it
Usually, we can determine these without actually writing a TM version of our procedure!

                                                                                          5
Orders of Growth
                   6
Asymptotic Operators




                       7
Asymptotic Operators

These notations define sets of functions


In computing, the function inside the operator is
(usually) a mapping from the size of the input to the
number of steps required. We use the asymptotic
operators to abstract away all the silly details about
particular computers.

                                                         8
Big O
Intuition: the set of functions that grow no
  faster than f (more formal definition soon)
Asymptotic growth rate: as input to f approaches
  infinity, how fast does value of f increase
  Hence, only the fastest-growing term in f matters.


                                ?
           ?
                                                       9
Examples
              O(n3)               f(n) = 12n2 + n

                          O(n2)
f(n) = n2.5




 f(n) = n3.1 – n2

                                                10
Formal Definition




                    11
O Examples




x O (x2)?
10x O (x)?
x2   O (x)?


                       12
Lower Bound:          (Omega)




         Only difference from O: this was


                                            13
Where is
 (n2)?
              O(n3)                  f(n) = 12n2 + n

                             O(n2)
f(n) = n2.5           (n2)




 f(n) = n3.1 – n2

                                                   14
Inside-Out
                (n3)                  f(n) = 12n2 + n

                               (n2)
f(n) = n2.5            O(n2)




 f(n) = n3.1 – n2

                                                    15
Recap
Big-O: functions that grow no faster than f




Omega ( ): functions that grow no slower than f




                                                  16
The Sets O(f ) and Ω(f )

                O(f)   Ω(f) Functions
    Functions                that grow
    that grow                no slower
    no faster            f   than f
    than f




                                         17
What else might be useful?

              O(n3)                  f(n) = 12n2 + n

                             O(n2)
f(n) = n2.5           (n2)




 f(n) = n3.1 – n2

                                                   18
Theta (“Order of”)
Intuition: set of functions that grow as fast as f

Definition:




Slang: When people say, “f is order g” that means


                                                     19
Tight Bound Theta ( )
              O(n3)                  f(n) = 12n2 + n

                             O(n2)
f(n) = n2.5           (n2)
                                     (n2)



 f(n) = n3.1 – n2             Faster Growing

                                                   20
Θ Examples
Is 10n in Θ(n)?
   Yes, since 10n is (n) and 10n is in O(n)
      Doesn’t matter that you choose different c values
        for each part; they are independent
Is n2 in Θ(n)?
    No, since n2 is not in O(n)
 Is n in Θ(n2)?
    No, since n2 is not in (n)


                                                          21
The Sets O(f ), Ω(f ), and Θ(f )

                                   O(f)   Ω(f) Functions
                     Functions                  that grow
                     that grow                  no slower
                     no faster              f   than f
                     than f




          Θ(f)


How big are O(f ), Ω(f ), and Θ(f )?

                                                            22
Summary
Big-O: grow no faster than f
Omega: grow no slower than f
Theta:


  Which of these would we most like to know about
    costproc(n) = number of steps to execute proc on
                     input of size n
  ?


                                                       23
Complexity of Problems
       So, why do we need O and Ω?

Computer scientists often care about the
complexity of problems not algorithms. The
complexity of a problem is the complexity of the
best possible algorithm that solves the problem.



                                                   24
Algorithm Analysis
What is the asymptotic running time of the
Scheme procedure below:

    (define (bigger a b)
      (if (> a b) a b))




                                             25
Charge
Next class:
analyzing the costs of bigger
analyzing the costs of brute-force-lorenz

     Assistant Coaches’ Review Sessions for Exam 1:
         Tuesday (tomorrow), 6:30pm, Rice 442
             Wednesday, 7:30pm, Rice 442




                                                      26

More Related Content

PPT
19 algorithms-and-complexity-110627100203-phpapp02
PPTX
C++ & Java JIT Optimizations: Finding Prime Numbers
PDF
Introduction to Polyhedral Compilation
PDF
Fast Identification of Heavy Hitters by Cached and Packed Group Testing
PDF
Let’s talk about microbenchmarking
PDF
Fast Wavelet Tree Construction in Practice
PDF
【論文紹介】Relay: A New IR for Machine Learning Frameworks
PDF
ICML2013読み会 Large-Scale Learning with Less RAM via Randomization
19 algorithms-and-complexity-110627100203-phpapp02
C++ & Java JIT Optimizations: Finding Prime Numbers
Introduction to Polyhedral Compilation
Fast Identification of Heavy Hitters by Cached and Packed Group Testing
Let’s talk about microbenchmarking
Fast Wavelet Tree Construction in Practice
【論文紹介】Relay: A New IR for Machine Learning Frameworks
ICML2013読み会 Large-Scale Learning with Less RAM via Randomization

What's hot (20)

PDF
Faster Practical Block Compression for Rank/Select Dictionaries
PPTX
PVS-Studio team experience: checking various open source projects, or mistake...
PDF
Ch01 basic concepts_nosoluiton
PDF
Codes and Isogenies
PDF
Yampa AFRP Introduction
PDF
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
PDF
2020 겨울방학 정기스터디 3주차
PPT
A verifiable random function with short proofs and keys
PDF
2020 1학기 정기스터디 1주차
PDF
Compilation of COSMO for GPU using LLVM
PDF
ZK Study Club: Sumcheck Arguments and Their Applications
PPTX
Improved security system using steganography and elliptic curve crypto...
PDF
The Uncertain Enterprise
PDF
8086 labmanual
PDF
zkStudy Club: Subquadratic SNARGs in the Random Oracle Model
PDF
Tiramisu をちょっと、味見してみました。
PDF
Python grass
PDF
Program implementation and testing
PDF
Introduction to Homomorphic Encryption
PPTX
Computing on Encrypted Data
Faster Practical Block Compression for Rank/Select Dictionaries
PVS-Studio team experience: checking various open source projects, or mistake...
Ch01 basic concepts_nosoluiton
Codes and Isogenies
Yampa AFRP Introduction
zkStudyClub: PLONKUP & Reinforced Concrete [Luke Pearson, Joshua Fitzgerald, ...
2020 겨울방학 정기스터디 3주차
A verifiable random function with short proofs and keys
2020 1학기 정기스터디 1주차
Compilation of COSMO for GPU using LLVM
ZK Study Club: Sumcheck Arguments and Their Applications
Improved security system using steganography and elliptic curve crypto...
The Uncertain Enterprise
8086 labmanual
zkStudy Club: Subquadratic SNARGs in the Random Oracle Model
Tiramisu をちょっと、味見してみました。
Python grass
Program implementation and testing
Introduction to Homomorphic Encryption
Computing on Encrypted Data
Ad

Similar to Class 18: Measuring Cost (20)

PPTX
Algo complexity
PDF
PDF
Algorithm chapter 2
PPTX
Asymptotic notations
PDF
Skiena algorithm 2007 lecture02 asymptotic notation
PDF
Data Structures - Lecture 8 - Study Notes
PPTX
1_Asymptotic_Notation_pptx.pptx
PPT
Design and analysis of algorithm ppt ppt
PDF
CS-102 DS-class_01_02 Lectures Data .pdf
PPTX
Asymptotic notations for desing analysis of algorithm.pptx
PPT
02-asymp.ppt YJTYJTYFHYTYHFHTFTHFHTFTHFTHTHFT
PDF
Asymptotic Notation
PPT
ASYMTOTIC NOTATION ON DATA STRUCTURE AND ALGORITHM
PDF
6_12_13Asymptotic analysiskfnhlkjsbfbkjs.pdf
PPT
DSA Asymptotic Notations and Functions.ppt
PPT
02-asymp.ppt01_Intro.ppt algorithm for preperation stu used
PPTX
Dr hasany 2467_16649_1_lec-2-zabist
PPT
AsymptoticAnalysis-goal of analysis of algorithms
PDF
Dynamic Programming From CS 6515(Fibonacci, LIS, LCS))
PPT
Time complexity
Algo complexity
Algorithm chapter 2
Asymptotic notations
Skiena algorithm 2007 lecture02 asymptotic notation
Data Structures - Lecture 8 - Study Notes
1_Asymptotic_Notation_pptx.pptx
Design and analysis of algorithm ppt ppt
CS-102 DS-class_01_02 Lectures Data .pdf
Asymptotic notations for desing analysis of algorithm.pptx
02-asymp.ppt YJTYJTYFHYTYHFHTFTHFHTFTHFTHTHFT
Asymptotic Notation
ASYMTOTIC NOTATION ON DATA STRUCTURE AND ALGORITHM
6_12_13Asymptotic analysiskfnhlkjsbfbkjs.pdf
DSA Asymptotic Notations and Functions.ppt
02-asymp.ppt01_Intro.ppt algorithm for preperation stu used
Dr hasany 2467_16649_1_lec-2-zabist
AsymptoticAnalysis-goal of analysis of algorithms
Dynamic Programming From CS 6515(Fibonacci, LIS, LCS))
Time complexity
Ad

More from David Evans (20)

PPTX
Cryptocurrency Jeopardy!
PPTX
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
PPTX
Hidden Services, Zero Knowledge
PPTX
Anonymity in Bitcoin
PPTX
Midterm Confirmations
PPTX
Scripting Transactions
PPTX
How to Live in Paradise
PPTX
Bitcoin Script
PPTX
Mining Economics
PPTX
Mining
PPTX
The Blockchain
PPTX
Becoming More Paranoid
PPTX
Asymmetric Key Signatures
PPTX
Introduction to Cryptography
PPTX
Class 1: What is Money?
PPTX
Multi-Party Computation for the Masses
PPTX
Proof of Reserve
PPTX
Silk Road
PPTX
Blooming Sidechains!
PPTX
Useful Proofs of Work, Permacoin
Cryptocurrency Jeopardy!
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Hidden Services, Zero Knowledge
Anonymity in Bitcoin
Midterm Confirmations
Scripting Transactions
How to Live in Paradise
Bitcoin Script
Mining Economics
Mining
The Blockchain
Becoming More Paranoid
Asymmetric Key Signatures
Introduction to Cryptography
Class 1: What is Money?
Multi-Party Computation for the Masses
Proof of Reserve
Silk Road
Blooming Sidechains!
Useful Proofs of Work, Permacoin

Recently uploaded (20)

PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Electronic commerce courselecture one. Pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
MYSQL Presentation for SQL database connectivity
Unlocking AI with Model Context Protocol (MCP)
Dropbox Q2 2025 Financial Results & Investor Presentation
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Chapter 3 Spatial Domain Image Processing.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
The AUB Centre for AI in Media Proposal.docx
Big Data Technologies - Introduction.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Empathic Computing: Creating Shared Understanding
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MYSQL Presentation for SQL database connectivity

Class 18: Measuring Cost

  • 1. Class 18: Measuring Cost cs1120 Fall 2011 David Evans Colossus Rebuilt, Bletchley Park, Summer 2004 3 October 2011
  • 2. Plan How Computer Scientists Measure Cost Asymptotic Operators Assistant Coaches’ Review Sessions for Exam 1: Tuesday (tomorrow), 6:30pm, Rice 442 Wednesday, 7:30pm, Rice 442 2
  • 3. (define (fibo-rec n) (define (fibo-loop n) (if (= n 0) 0 (cdr (if (= n 1) 1 (loop 1 (cons 0 1) (lambda (i) (< i n)) inc (+ (fibo-rec (- n 1)) (lambda (i v) (fibo-rec (- n 2)))))) (cons (cdr v) (+ (car v) (cdr v))))))) > (time (fibo-rec 2)) > (time (fibo-loop 2)) cpu time: 0 real time: 0 gc time: 0 cpu time: 0 real time: 0 gc time: 0 1 1 > (time (fibo-rec 5)) > (time (fibo-loop 5)) cpu time: 0 real time: 0 gc time: 0 cpu time: 0 real time: 0 gc time: 0 5 5 > (time (fibo-rec 20)) > (time (fibo-loop 20)) cpu time: 0 real time: 2 gc time: 0 cpu time: 0 real time: 0 gc time: 0 6765 6765 > (time (fibo-rec 30)) > (time (fibo-loop 30)) cpu time: 359 real time: 370 gc time: 0 cpu time: 0 real time: 0 gc time: 0 832040 832040 > (time (fibo-rec 60)) > (time (fibo-loop 60)) still waiting since Friday… cpu time: 0 real time: 0 gc time: 0 1548008755920
  • 4. (define (fibo-rec n) (define (fibo-loop n) (if (= n 0) 0 (cdr (if (= n 1) 1 (loop 1 (cons 0 1) (+ (fibo-rec (- n 1)) (lambda (i) (< i n)) inc (fibo-rec (- n 2)))))) (lambda (i v) (cons (cdr v) (+ (car v) (cdr v))))))) Number of “expensive” calls: Number of “expensive” calls: where n is the value of the input, and is the “golden ratio”:
  • 5. How Computer Scientists Measure Cost Abstract: hide all the details that will change when you get your next laptop to capture the fundamental cost of the procedure Cost: number of steps for a Turing Machine to execute the procedure Order of Growth: what matters is how the cost scales with the size of the input Size of input: how many TM squares needed to represent it Usually, we can determine these without actually writing a TM version of our procedure! 5
  • 8. Asymptotic Operators These notations define sets of functions In computing, the function inside the operator is (usually) a mapping from the size of the input to the number of steps required. We use the asymptotic operators to abstract away all the silly details about particular computers. 8
  • 9. Big O Intuition: the set of functions that grow no faster than f (more formal definition soon) Asymptotic growth rate: as input to f approaches infinity, how fast does value of f increase Hence, only the fastest-growing term in f matters. ? ? 9
  • 10. Examples O(n3) f(n) = 12n2 + n O(n2) f(n) = n2.5 f(n) = n3.1 – n2 10
  • 12. O Examples x O (x2)? 10x O (x)? x2 O (x)? 12
  • 13. Lower Bound: (Omega) Only difference from O: this was 13
  • 14. Where is (n2)? O(n3) f(n) = 12n2 + n O(n2) f(n) = n2.5 (n2) f(n) = n3.1 – n2 14
  • 15. Inside-Out (n3) f(n) = 12n2 + n (n2) f(n) = n2.5 O(n2) f(n) = n3.1 – n2 15
  • 16. Recap Big-O: functions that grow no faster than f Omega ( ): functions that grow no slower than f 16
  • 17. The Sets O(f ) and Ω(f ) O(f) Ω(f) Functions Functions that grow that grow no slower no faster f than f than f 17
  • 18. What else might be useful? O(n3) f(n) = 12n2 + n O(n2) f(n) = n2.5 (n2) f(n) = n3.1 – n2 18
  • 19. Theta (“Order of”) Intuition: set of functions that grow as fast as f Definition: Slang: When people say, “f is order g” that means 19
  • 20. Tight Bound Theta ( ) O(n3) f(n) = 12n2 + n O(n2) f(n) = n2.5 (n2) (n2) f(n) = n3.1 – n2 Faster Growing 20
  • 21. Θ Examples Is 10n in Θ(n)? Yes, since 10n is (n) and 10n is in O(n) Doesn’t matter that you choose different c values for each part; they are independent Is n2 in Θ(n)? No, since n2 is not in O(n) Is n in Θ(n2)? No, since n2 is not in (n) 21
  • 22. The Sets O(f ), Ω(f ), and Θ(f ) O(f) Ω(f) Functions Functions that grow that grow no slower no faster f than f than f Θ(f) How big are O(f ), Ω(f ), and Θ(f )? 22
  • 23. Summary Big-O: grow no faster than f Omega: grow no slower than f Theta: Which of these would we most like to know about costproc(n) = number of steps to execute proc on input of size n ? 23
  • 24. Complexity of Problems So, why do we need O and Ω? Computer scientists often care about the complexity of problems not algorithms. The complexity of a problem is the complexity of the best possible algorithm that solves the problem. 24
  • 25. Algorithm Analysis What is the asymptotic running time of the Scheme procedure below: (define (bigger a b) (if (> a b) a b)) 25
  • 26. Charge Next class: analyzing the costs of bigger analyzing the costs of brute-force-lorenz Assistant Coaches’ Review Sessions for Exam 1: Tuesday (tomorrow), 6:30pm, Rice 442 Wednesday, 7:30pm, Rice 442 26