SlideShare a Scribd company logo
Class 24:
Imperative
Programming
    cs1120 Fall 2011
    David Evans
    19 October 2011
Menu                                      Running time
                                                        practice throughout!

Recap: list-append vs. mlist-append!
Aliasing
mlist-reverse!

I haven’t yet written solutions to the Chapter 9 Exercises, but I will do it soon if
at least 3 people provide some evidence that they are trying to solve them!
You can do this by posting your solutions or questions as comments on today’s
class. I also how to see more comments/suggestions for the ElevenLearning
version of the book (post here: http://www.cs.virginia.edu/cs1120/all/2011/on-
line-version-of-course-book). If you do at least one of these, you can turn in
PS5 at Monday’s class with no penalty.
Appending vs. Appending!
(define (list-append p q)          (define (mlist-append! p q)
 (if (null? p) q                     (if (null? p)
     (cons
      (car p)                          (if (null? (mcdr p))
      (list-append (cdr p) q))))
                                          (mlist-append! (mcdr p) q))))


 Running time:




 Memory use:



                                                                          3
Appending vs. Appending!
                                   (define (mlist-append! p q)
(define (list-append p q)
                                     (if (null? p)
 (if (null? p) q
                                         (error “Cannot append to empty!”)
     (cons
                                         (if (null? (mcdr p))
      (car p)
                                             (set-mcdr! p q)
      (list-append (cdr p) q))))
                                             (mlist-append! (mcdr p) q))))

  Running time in
   (Np), Np is number of            Running time in
  elements in p                       (Np), number of elements
                                    in p
  Number of new cons
  cells: (Np)                       Number of new cons cells: 0
                                                                         4
Does it matter?
To test: Define a procedure to make a list of length n.




                                                          5
(define (make-list n)
                                                      (if (= n 0) null
> (define p1 (make-list 100000))
                                                          (cons 0 (make-list (- n 1)))))
> (define p2 (make-list 100000))
> (time (begin (list-append p1 p2) (void)))
cpu time: 78 real time: 77 gc time: 47           (define (make-mlist n)
> (length p1)                                      (if (= n 0) null
100000                                                 (mcons 0 (make-mlist (- n 1)))))
> (define m1 (make-mlist 100000))
> (define m2 (make-mlist 100000))        > (time (begin (list-append p1 p2) (void)))
> (time (mlist-append! m1 m2))           cpu time: 78 real time: 77 gc time: 47
cpu time: 0 real time: 13 gc time: 0     > (time (begin (list-append p1 p2) (void)))
> (mlength m1)                           cpu time: 78 real time: 83 gc time: 31
200000                                   > (time (begin (list-append p1 p2) (void)))
> (time (mlist-append! m1 m2))           cpu time: 78 real time: 73 gc time: 31
                                         > (time (begin (list-append p1 p2) (void)))
cpu time: 31 real time: 26 gc time: 0
                                         cpu time: 93 real time: 90 gc time: 31
> (time (mlist-append! m1 m2))




                                                                                           6
> (time (begin (append p1 p2) (void)))
                                     cpu time: 0 real time: 1 gc time: 0
                                     > (time (begin (append p1 p2) (void)))
                                     cpu time: 0 real time: 2 gc time: 0
                                     > (define p200k (append p1 p2))
                                     > (define p400k (append p200k p200k))
                                     > (define p800k (append p400k p400k))
What about                           > (define p1600k (append p800k p800k))
                                     > (length p1600k)

the built-in                         1600000
                                     > (time (begin (append p400k p400k) (void)))
                                     cpu time: 0 real time: 6 gc time: 0
 append?                             > (time (begin (append p800k p800k) (void)))
                                     cpu time: 109 real time: 110 gc time: 94
                                     > (time (begin (append p1600k p1600k) (void)))
                                     cpu time: 1045 real time: 1044 gc time: 999
                                     > (time (begin (append p1600k p1600k) (void)))
Possible reason built-in append is so much faster:real time: 160 gc time: 125
                                     cpu time: 171
Internal list representation doesn’t > (time (begin (append p1600k p1600k) (void)))
                                     have to be just cons
pairs (could jump quickly over many cells) 203 real time: 197 gc time: 171
                                     cpu time:
Because lists are immutable, doesn’t have to copy list!


                                                                                      7
What about the built-in mappend!
> (require racket/mpair)
> (time (begin (mappend! m1 m2) (void)))
cpu time: 0 real time: 1 gc time: 0
> (time (begin (mappend! m1 m2) (void)))
cpu time: 0 real time: 1 gc time: 0
> (time (begin (mappend! m1 m2) (void)))



                                           8
Still waiting…
Favorite response to Exam Question 14:
What topics to you hope to see in the remainder of the
  course?:

  “I can’t even begin to anticipate
  what will come next – most of
  what we’ve done so far has
  already blown my mind.”

                                                         9
10
Why We’re Still Waiting / Blowing our Minds?
        m1:

                   1            2         3



                          m2:
                                      4            5         6


(define (mlist-append! p q)
  (if (null? p)                               (mlist-append! m1 m2)
      (error “Cannot append to empty!”)       (mlist-append! m1 m2)
      (if (null? (mcdr p))                    (mlist-append! m1 m2)
          (set-mcdr! p q)
          (mlist-append! (mcdr p) q))))
Aliasing
        m1:

                      1         2       3



> (mlist-append! m1 m2)   m2:
> m1                                7
                                    4       5   6
{1 2 3 4 5 6}
> m2
{4 5 6}
> (set-mcar! m2 7)
> m2
{7 5 6}
> m1
{1 2 3 7 5 6}
Aliasing Problems
    m1:

                  1             2              3



                          m2:
                                           4             5                6




> (mlist-append! m1 m2)
> (mlist-append! m1 m2)
> (mlist-length m1)                 (define (mlist-length p)
                                     (if (null? p) 0
                                        (+ 1 (mlist-length (mcdr p)))))
Reversing




            14
Analyzing list-reverse
(define (list-reverse p)
  (if (null? p) null
      (list-append (list-reverse (cdr p)) (list (car p)))))




                                                              15
Reversing
(define (list-reverse p)
  (if (null? p) null
      (list-append (list-reverse (cdr p)) (list (car p)))))

Running time is in (N2)
  where N is number of elements in p.
Number of new cons cells:
 for the list-appends: N-1 + N-2 + … + 1 = N2/ 2
 + for the (list (car p)): N
 memory use is in (N2)


                                                              16
mlist-reverse!
Define a mlist-reverse! that reverses the elements of a
mutable list. The output should be a mutable list with
the elements in the reverse order. The number of cons
cells it creates should not scale with the length of the
list. (The input list can be mutated arbitrarily!)




                                                           17
Charge
PS5 Due Friday
  Except as noted earlier: if you post a
  comment/suggestion about the
  ElevenLearning book or a comment that
  suggests you are trying the Chapter 9 book
  exercises then it is due Monday

More Related Content

What's hot

Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016
Steffen Wenz
 
Go concurrency
Go concurrencyGo concurrency
Go concurrency
siuyin
 
Procesos
ProcesosProcesos
Procesos
PublioScipion
 
Concurrency in Golang
Concurrency in GolangConcurrency in Golang
Concurrency in Golang
Oliver N
 
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Gregg Donovan
 
Cluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in PracticeCluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in Practice
Steffen Wenz
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
Steffen Wenz
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
Designveloper
 
R/C++ talk at earl 2014
R/C++ talk at earl 2014R/C++ talk at earl 2014
R/C++ talk at earl 2014
Romain Francois
 
Gc in golang
Gc in golangGc in golang
Gc in golang
Genchi Lu
 
CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogrammingdudarev
 
Implementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HWImplementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HW
Joe Jiang
 
Gc in golang
Gc in golangGc in golang
Gc in golang
Genchi Lu
 
Groovy
GroovyGroovy
Groovy
Zen Urban
 
計算機性能の限界点とその考え方
計算機性能の限界点とその考え方計算機性能の限界点とその考え方
計算機性能の限界点とその考え方
Naoto MATSUMOTO
 
Data structure programs in c++
Data structure programs in c++Data structure programs in c++
Data structure programs in c++mmirfan
 
Concurrency in Go by Denys Goldiner.pdf
Concurrency in Go by Denys Goldiner.pdfConcurrency in Go by Denys Goldiner.pdf
Concurrency in Go by Denys Goldiner.pdf
Denys Goldiner
 
Multi qubit entanglement
Multi qubit entanglementMulti qubit entanglement
Multi qubit entanglement
Vijayananda Mohire
 
Understanding the nodejs event loop
Understanding the nodejs event loopUnderstanding the nodejs event loop
Understanding the nodejs event loop
Saurabh Kumar
 

What's hot (20)

Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016Powered by Python - PyCon Germany 2016
Powered by Python - PyCon Germany 2016
 
Go concurrency
Go concurrencyGo concurrency
Go concurrency
 
Procesos
ProcesosProcesos
Procesos
 
Concurrency in Golang
Concurrency in GolangConcurrency in Golang
Concurrency in Golang
 
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
Living with Garbage by Gregg Donovan at LuceneSolr Revolution 2013
 
Cluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in PracticeCluj Big Data Meetup - Big Data in Practice
Cluj Big Data Meetup - Big Data in Practice
 
Cluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in CCluj.py Meetup: Extending Python in C
Cluj.py Meetup: Extending Python in C
 
JavaScript Event Loop
JavaScript Event LoopJavaScript Event Loop
JavaScript Event Loop
 
R/C++ talk at earl 2014
R/C++ talk at earl 2014R/C++ talk at earl 2014
R/C++ talk at earl 2014
 
Gc in golang
Gc in golangGc in golang
Gc in golang
 
CL metaprogramming
CL metaprogrammingCL metaprogramming
CL metaprogramming
 
Implementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HWImplementation of Bitcoin Miner on SW and HW
Implementation of Bitcoin Miner on SW and HW
 
Gc in golang
Gc in golangGc in golang
Gc in golang
 
Groovy
GroovyGroovy
Groovy
 
計算機性能の限界点とその考え方
計算機性能の限界点とその考え方計算機性能の限界点とその考え方
計算機性能の限界点とその考え方
 
Data structure programs in c++
Data structure programs in c++Data structure programs in c++
Data structure programs in c++
 
Concurrency in Go by Denys Goldiner.pdf
Concurrency in Go by Denys Goldiner.pdfConcurrency in Go by Denys Goldiner.pdf
Concurrency in Go by Denys Goldiner.pdf
 
R snippet
R snippetR snippet
R snippet
 
Multi qubit entanglement
Multi qubit entanglementMulti qubit entanglement
Multi qubit entanglement
 
Understanding the nodejs event loop
Understanding the nodejs event loopUnderstanding the nodejs event loop
Understanding the nodejs event loop
 

Viewers also liked

Imperative programming
Imperative programmingImperative programming
Imperative programming
Edward Blurock
 
Functional Programming with Python (RuPy 2008)
Functional Programming with Python (RuPy 2008)Functional Programming with Python (RuPy 2008)
Functional Programming with Python (RuPy 2008)
Adam Byrtek
 
Prgramming paradigms
Prgramming paradigmsPrgramming paradigms
Prgramming paradigms
Anirudh Chauhan
 
Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?
Netguru
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
Shahriar Hyder
 
Programming languages
Programming languagesProgramming languages
Programming languages
Asmasum
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languagesVarun Garg
 

Viewers also liked (7)

Imperative programming
Imperative programmingImperative programming
Imperative programming
 
Functional Programming with Python (RuPy 2008)
Functional Programming with Python (RuPy 2008)Functional Programming with Python (RuPy 2008)
Functional Programming with Python (RuPy 2008)
 
Prgramming paradigms
Prgramming paradigmsPrgramming paradigms
Prgramming paradigms
 
Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?Programming Paradigms Which One Is The Best?
Programming Paradigms Which One Is The Best?
 
Functional Programming Fundamentals
Functional Programming FundamentalsFunctional Programming Fundamentals
Functional Programming Fundamentals
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 

Similar to Class 24: Imperative Programming

Class 10: Abstracting Procedures
Class 10: Abstracting ProceduresClass 10: Abstracting Procedures
Class 10: Abstracting Procedures
David Evans
 
Do snow.rwn
Do snow.rwnDo snow.rwn
Do snow.rwn
ARUN DN
 
Class 28: Entropy
Class 28: EntropyClass 28: Entropy
Class 28: Entropy
David Evans
 
Hadoop I/O Analysis
Hadoop I/O AnalysisHadoop I/O Analysis
Hadoop I/O Analysis
Richard McDougall
 
Lisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligenceLisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligence
ArtiSolanki5
 
Time Series Analysis for Network Secruity
Time Series Analysis for Network SecruityTime Series Analysis for Network Secruity
Time Series Analysis for Network Secruity
mrphilroth
 
Class 25: Reversing Reverse
Class 25: Reversing ReverseClass 25: Reversing Reverse
Class 25: Reversing Reverse
David Evans
 
Python Performance 101
Python Performance 101Python Performance 101
Python Performance 101Ankur Gupta
 
Parallel Computing in R
Parallel Computing in RParallel Computing in R
Parallel Computing in R
mickey24
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
Knoldus Inc.
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
Class 18: Measuring Cost
Class 18: Measuring CostClass 18: Measuring Cost
Class 18: Measuring Cost
David Evans
 
SICP勉強会について
SICP勉強会についてSICP勉強会について
SICP勉強会について
Yusuke Sasaki
 
Monadologie
MonadologieMonadologie
Monadologie
league
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
Databricks
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
Databricks
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
OCamlOScope: a New OCaml API Search
OCamlOScope: a New OCaml API SearchOCamlOScope: a New OCaml API Search
OCamlOScope: a New OCaml API Search
Jun Furuse
 
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
Murali Kummitha
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
Murali Kummitha
 

Similar to Class 24: Imperative Programming (20)

Class 10: Abstracting Procedures
Class 10: Abstracting ProceduresClass 10: Abstracting Procedures
Class 10: Abstracting Procedures
 
Do snow.rwn
Do snow.rwnDo snow.rwn
Do snow.rwn
 
Class 28: Entropy
Class 28: EntropyClass 28: Entropy
Class 28: Entropy
 
Hadoop I/O Analysis
Hadoop I/O AnalysisHadoop I/O Analysis
Hadoop I/O Analysis
 
Lisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligenceLisp and prolog in artificial intelligence
Lisp and prolog in artificial intelligence
 
Time Series Analysis for Network Secruity
Time Series Analysis for Network SecruityTime Series Analysis for Network Secruity
Time Series Analysis for Network Secruity
 
Class 25: Reversing Reverse
Class 25: Reversing ReverseClass 25: Reversing Reverse
Class 25: Reversing Reverse
 
Python Performance 101
Python Performance 101Python Performance 101
Python Performance 101
 
Parallel Computing in R
Parallel Computing in RParallel Computing in R
Parallel Computing in R
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
Class 18: Measuring Cost
Class 18: Measuring CostClass 18: Measuring Cost
Class 18: Measuring Cost
 
SICP勉強会について
SICP勉強会についてSICP勉強会について
SICP勉強会について
 
Monadologie
MonadologieMonadologie
Monadologie
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 
User Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love StoryUser Defined Aggregation in Apache Spark: A Love Story
User Defined Aggregation in Apache Spark: A Love Story
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
OCamlOScope: a New OCaml API Search
OCamlOScope: a New OCaml API SearchOCamlOScope: a New OCaml API Search
OCamlOScope: a New OCaml API Search
 
Os lab upto 1st mid
Os lab upto 1st midOs lab upto 1st mid
Os lab upto 1st mid
 
Os lab 1st mid
Os lab 1st midOs lab 1st mid
Os lab 1st mid
 

More from David Evans

Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!
David Evans
 
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksTrick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
David Evans
 
Hidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeHidden Services, Zero Knowledge
Hidden Services, Zero Knowledge
David Evans
 
Anonymity in Bitcoin
Anonymity in BitcoinAnonymity in Bitcoin
Anonymity in Bitcoin
David Evans
 
Midterm Confirmations
Midterm ConfirmationsMidterm Confirmations
Midterm Confirmations
David Evans
 
Scripting Transactions
Scripting TransactionsScripting Transactions
Scripting Transactions
David Evans
 
How to Live in Paradise
How to Live in ParadiseHow to Live in Paradise
How to Live in Paradise
David Evans
 
Bitcoin Script
Bitcoin ScriptBitcoin Script
Bitcoin Script
David Evans
 
Mining Economics
Mining EconomicsMining Economics
Mining Economics
David Evans
 
Mining
MiningMining
Mining
David Evans
 
The Blockchain
The BlockchainThe Blockchain
The Blockchain
David Evans
 
Becoming More Paranoid
Becoming More ParanoidBecoming More Paranoid
Becoming More Paranoid
David Evans
 
Asymmetric Key Signatures
Asymmetric Key SignaturesAsymmetric Key Signatures
Asymmetric Key Signatures
David Evans
 
Introduction to Cryptography
Introduction to CryptographyIntroduction to Cryptography
Introduction to Cryptography
David Evans
 
Class 1: What is Money?
Class 1: What is Money?Class 1: What is Money?
Class 1: What is Money?
David Evans
 
Multi-Party Computation for the Masses
Multi-Party Computation for the MassesMulti-Party Computation for the Masses
Multi-Party Computation for the Masses
David Evans
 
Proof of Reserve
Proof of ReserveProof of Reserve
Proof of Reserve
David Evans
 
Silk Road
Silk RoadSilk Road
Silk Road
David Evans
 
Blooming Sidechains!
Blooming Sidechains!Blooming Sidechains!
Blooming Sidechains!
David Evans
 
Useful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinUseful Proofs of Work, Permacoin
Useful Proofs of Work, Permacoin
David Evans
 

More from David Evans (20)

Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!Cryptocurrency Jeopardy!
Cryptocurrency Jeopardy!
 
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for CypherpunksTrick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
Trick or Treat?: Bitcoin for Non-Believers, Cryptocurrencies for Cypherpunks
 
Hidden Services, Zero Knowledge
Hidden Services, Zero KnowledgeHidden Services, Zero Knowledge
Hidden Services, Zero Knowledge
 
Anonymity in Bitcoin
Anonymity in BitcoinAnonymity in Bitcoin
Anonymity in Bitcoin
 
Midterm Confirmations
Midterm ConfirmationsMidterm Confirmations
Midterm Confirmations
 
Scripting Transactions
Scripting TransactionsScripting Transactions
Scripting Transactions
 
How to Live in Paradise
How to Live in ParadiseHow to Live in Paradise
How to Live in Paradise
 
Bitcoin Script
Bitcoin ScriptBitcoin Script
Bitcoin Script
 
Mining Economics
Mining EconomicsMining Economics
Mining Economics
 
Mining
MiningMining
Mining
 
The Blockchain
The BlockchainThe Blockchain
The Blockchain
 
Becoming More Paranoid
Becoming More ParanoidBecoming More Paranoid
Becoming More Paranoid
 
Asymmetric Key Signatures
Asymmetric Key SignaturesAsymmetric Key Signatures
Asymmetric Key Signatures
 
Introduction to Cryptography
Introduction to CryptographyIntroduction to Cryptography
Introduction to Cryptography
 
Class 1: What is Money?
Class 1: What is Money?Class 1: What is Money?
Class 1: What is Money?
 
Multi-Party Computation for the Masses
Multi-Party Computation for the MassesMulti-Party Computation for the Masses
Multi-Party Computation for the Masses
 
Proof of Reserve
Proof of ReserveProof of Reserve
Proof of Reserve
 
Silk Road
Silk RoadSilk Road
Silk Road
 
Blooming Sidechains!
Blooming Sidechains!Blooming Sidechains!
Blooming Sidechains!
 
Useful Proofs of Work, Permacoin
Useful Proofs of Work, PermacoinUseful Proofs of Work, Permacoin
Useful Proofs of Work, Permacoin
 

Recently uploaded

Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 

Recently uploaded (20)

Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 

Class 24: Imperative Programming

  • 1. Class 24: Imperative Programming cs1120 Fall 2011 David Evans 19 October 2011
  • 2. Menu Running time practice throughout! Recap: list-append vs. mlist-append! Aliasing mlist-reverse! I haven’t yet written solutions to the Chapter 9 Exercises, but I will do it soon if at least 3 people provide some evidence that they are trying to solve them! You can do this by posting your solutions or questions as comments on today’s class. I also how to see more comments/suggestions for the ElevenLearning version of the book (post here: http://www.cs.virginia.edu/cs1120/all/2011/on- line-version-of-course-book). If you do at least one of these, you can turn in PS5 at Monday’s class with no penalty.
  • 3. Appending vs. Appending! (define (list-append p q) (define (mlist-append! p q) (if (null? p) q (if (null? p) (cons (car p) (if (null? (mcdr p)) (list-append (cdr p) q)))) (mlist-append! (mcdr p) q)))) Running time: Memory use: 3
  • 4. Appending vs. Appending! (define (mlist-append! p q) (define (list-append p q) (if (null? p) (if (null? p) q (error “Cannot append to empty!”) (cons (if (null? (mcdr p)) (car p) (set-mcdr! p q) (list-append (cdr p) q)))) (mlist-append! (mcdr p) q)))) Running time in (Np), Np is number of Running time in elements in p (Np), number of elements in p Number of new cons cells: (Np) Number of new cons cells: 0 4
  • 5. Does it matter? To test: Define a procedure to make a list of length n. 5
  • 6. (define (make-list n) (if (= n 0) null > (define p1 (make-list 100000)) (cons 0 (make-list (- n 1))))) > (define p2 (make-list 100000)) > (time (begin (list-append p1 p2) (void))) cpu time: 78 real time: 77 gc time: 47 (define (make-mlist n) > (length p1) (if (= n 0) null 100000 (mcons 0 (make-mlist (- n 1))))) > (define m1 (make-mlist 100000)) > (define m2 (make-mlist 100000)) > (time (begin (list-append p1 p2) (void))) > (time (mlist-append! m1 m2)) cpu time: 78 real time: 77 gc time: 47 cpu time: 0 real time: 13 gc time: 0 > (time (begin (list-append p1 p2) (void))) > (mlength m1) cpu time: 78 real time: 83 gc time: 31 200000 > (time (begin (list-append p1 p2) (void))) > (time (mlist-append! m1 m2)) cpu time: 78 real time: 73 gc time: 31 > (time (begin (list-append p1 p2) (void))) cpu time: 31 real time: 26 gc time: 0 cpu time: 93 real time: 90 gc time: 31 > (time (mlist-append! m1 m2)) 6
  • 7. > (time (begin (append p1 p2) (void))) cpu time: 0 real time: 1 gc time: 0 > (time (begin (append p1 p2) (void))) cpu time: 0 real time: 2 gc time: 0 > (define p200k (append p1 p2)) > (define p400k (append p200k p200k)) > (define p800k (append p400k p400k)) What about > (define p1600k (append p800k p800k)) > (length p1600k) the built-in 1600000 > (time (begin (append p400k p400k) (void))) cpu time: 0 real time: 6 gc time: 0 append? > (time (begin (append p800k p800k) (void))) cpu time: 109 real time: 110 gc time: 94 > (time (begin (append p1600k p1600k) (void))) cpu time: 1045 real time: 1044 gc time: 999 > (time (begin (append p1600k p1600k) (void))) Possible reason built-in append is so much faster:real time: 160 gc time: 125 cpu time: 171 Internal list representation doesn’t > (time (begin (append p1600k p1600k) (void))) have to be just cons pairs (could jump quickly over many cells) 203 real time: 197 gc time: 171 cpu time: Because lists are immutable, doesn’t have to copy list! 7
  • 8. What about the built-in mappend! > (require racket/mpair) > (time (begin (mappend! m1 m2) (void))) cpu time: 0 real time: 1 gc time: 0 > (time (begin (mappend! m1 m2) (void))) cpu time: 0 real time: 1 gc time: 0 > (time (begin (mappend! m1 m2) (void))) 8
  • 9. Still waiting… Favorite response to Exam Question 14: What topics to you hope to see in the remainder of the course?: “I can’t even begin to anticipate what will come next – most of what we’ve done so far has already blown my mind.” 9
  • 10. 10
  • 11. Why We’re Still Waiting / Blowing our Minds? m1: 1 2 3 m2: 4 5 6 (define (mlist-append! p q) (if (null? p) (mlist-append! m1 m2) (error “Cannot append to empty!”) (mlist-append! m1 m2) (if (null? (mcdr p)) (mlist-append! m1 m2) (set-mcdr! p q) (mlist-append! (mcdr p) q))))
  • 12. Aliasing m1: 1 2 3 > (mlist-append! m1 m2) m2: > m1 7 4 5 6 {1 2 3 4 5 6} > m2 {4 5 6} > (set-mcar! m2 7) > m2 {7 5 6} > m1 {1 2 3 7 5 6}
  • 13. Aliasing Problems m1: 1 2 3 m2: 4 5 6 > (mlist-append! m1 m2) > (mlist-append! m1 m2) > (mlist-length m1) (define (mlist-length p) (if (null? p) 0 (+ 1 (mlist-length (mcdr p)))))
  • 14. Reversing 14
  • 15. Analyzing list-reverse (define (list-reverse p) (if (null? p) null (list-append (list-reverse (cdr p)) (list (car p))))) 15
  • 16. Reversing (define (list-reverse p) (if (null? p) null (list-append (list-reverse (cdr p)) (list (car p))))) Running time is in (N2) where N is number of elements in p. Number of new cons cells: for the list-appends: N-1 + N-2 + … + 1 = N2/ 2 + for the (list (car p)): N memory use is in (N2) 16
  • 17. mlist-reverse! Define a mlist-reverse! that reverses the elements of a mutable list. The output should be a mutable list with the elements in the reverse order. The number of cons cells it creates should not scale with the length of the list. (The input list can be mutated arbitrarily!) 17
  • 18. Charge PS5 Due Friday Except as noted earlier: if you post a comment/suggestion about the ElevenLearning book or a comment that suggests you are trying the Chapter 9 book exercises then it is due Monday