Advertisement
timothy235

sicp-4-1-7-separating-syntactic-analysis-from-execution

Mar 16th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.41 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;;;;;;;;;;
  4. ;; 4.22 ;;
  5. ;;;;;;;;;;
  6.  
  7. ;; We only need to add a clause to analyze instructing it to recurse on
  8. ;; let->combination:
  9.  
  10.     ;; [(let? expr) (analyze (let->combination expr))]
  11.  
  12. ;; Otherwise, nothing else needs to change.
  13.  
  14. ;;;;;;;;;;
  15. ;; 4.23 ;;
  16. ;;;;;;;;;;
  17.  
  18. ;; Alyssa's code performs unnecessary lambda wrapping and so is less efficient than
  19. ;; the book code.
  20.  
  21. ;; For example, if procs contains just the one procedure p, Alyssa's code returns
  22. ;; (lambda (env) (p env)), whereas the book code simply returns p.
  23.  
  24. ;; And in the case where procs consists of the two procedures p and q, Alyssa's code
  25. ;; returns (lambda (env) (p env) (lambda (env) (q env))), whereas the book code
  26. ;; simply returns (lambda (env) (p env) (q env)).
  27.  
  28. ;;;;;;;;;;
  29. ;; 4.24 ;;
  30. ;;;;;;;;;;
  31.  
  32. ;; We'll compare how long the two interpreters take to compute some values of the
  33. ;; recursive fibonacci function.
  34.  
  35. ;; Comment out one of these before running the other.
  36. ;; (require "4-1-4-repl-program.rkt")
  37. (require "4-1-7-repl-program.rkt")
  38.  
  39. (my-eval '(define (fib n)
  40.             (cond [(= n 1) 1]
  41.                   [(= n 0) 0]
  42.                   [else (+ (fib (- n 1)) (fib (- n 2)))]))
  43.          the-global-environment)
  44. ;; 'ok
  45.  
  46. ;; ;; NO SEPARATE SYNTACTIC ANALYSIS
  47. ;; (time (my-eval '(fib 15) the-global-environment))
  48. ;; ;; cpu time: 46 real time: 56 gc time: 0
  49. ;; ;; 610
  50. ;; (time (my-eval '(fib 20) the-global-environment))
  51. ;; ;; cpu time: 688 real time: 680 gc time: 63
  52. ;; ;; 6765
  53. ;; (time (my-eval '(fib 25) the-global-environment))
  54. ;; ;; cpu time: 7016 real time: 7068 gc time: 205
  55. ;; ;; 75025
  56. ;; (time (my-eval '(fib 30) the-global-environment))
  57. ;; ;; cpu time: 76875 real time: 77585 gc time: 1803
  58. ;; ;; 832040
  59.  
  60. ;; WITH SEPARATE SYNTACTIC ANALYSIS
  61. (time (my-eval '(fib 15) the-global-environment))
  62. ;; cpu time: 31 real time: 27 gc time: 0
  63. ;; 610
  64. (time (my-eval '(fib 20) the-global-environment))
  65. ;; cpu time: 328 real time: 338 gc time: 31
  66. ;; 6765
  67. (time (my-eval '(fib 25) the-global-environment))
  68. ;; cpu time: 3406 real time: 3435 gc time: 126
  69. ;; 75025
  70. (time (my-eval '(fib 30) the-global-environment))
  71. ;; cpu time: 38234 real time: 39212 gc time: 1657
  72. ;; 832040
  73.  
  74. ;; Of course recursive fibonacci is exponential, an additive increase in input
  75. ;; results in a multiplicative increase in run time.  Even so, separating out
  76. ;; syntactic analysis has cut the run times in half, a significant savings.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement