2014年1月10日金曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の5(レジスタ計算機での計算)、5.5(翻訳系)、翻訳系の概観、5.5.7(翻訳したコードと評価器のインターフェース)、解釈と翻訳、問題 5.46.を解いてみる。

その他参考書籍

問題 5.46.

解釈する性能の計測には問題5.29を参照。register5.14.scmは問題5.14のコード。

コード(BBEdit)

sample.scm

(load "./eval.scm")
(load "./compiler5.44.scm")

(compile-and-go
'(define (fib n)
   (if (< n 2)
       n
       (+ (fib (- n 1)) (fib (- n 2))))))

入出力結果(Terminal, REPL(Read, Eval, Print, Loop))

$ scheme
MIT/GNU Scheme running under MacOSX
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright (C) 2011 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Saturday October 26, 2013 at 11:02:50 PM
  Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/C 4.118
  Edwin 3.116

1 ]=> (load "./eceval.scm")

;Loading "./eceval.scm"... done
;Value: start-eceval

1 ]=> (load "./sample.scm")

;Loading "./sample.scm"...
(total-pushes = 0 maximum-depth = 0)
;;; EC-Eval value:
ok

;;; EC-Eval input:
(fib 0)

(total-pushes = 7 maximum-depth = 3)
;;; EC-Eval value:
0

;;; EC-Eval input:
(fib 1)

(total-pushes = 7 maximum-depth = 3)
;;; EC-Eval value:
1

;;; EC-Eval input:
(fib 2)

(total-pushes = 17 maximum-depth = 5)
;;; EC-Eval value:
1

;;; EC-Eval input:
(fib 3)

(total-pushes = 27 maximum-depth = 8)
;;; EC-Eval value:
2

;;; EC-Eval input:
(fib 4)

(total-pushes = 47 maximum-depth = 11)
;;; EC-Eval value:
3

;;; EC-Eval input:
(fib 5)

(total-pushes = 77 maximum-depth = 14)
;;; EC-Eval value:
5

;;; EC-Eval input:
(fib 6)

(total-pushes = 127 maximum-depth = 17)
;;; EC-Eval value:
8

;;; EC-Eval input:
(fib 7)

(total-pushes = 207 maximum-depth = 20)
;;; EC-Eval value:
13

;;; EC-Eval input:
(fib 8)

(total-pushes = 337 maximum-depth = 23)
;;; EC-Eval value:
21

;;; EC-Eval input:
(fib 9)

(total-pushes = 547 maximum-depth = 26)
;;; EC-Eval value:
34

;;; EC-Eval input:
(fib 10)

(total-pushes = 887 maximum-depth = 29)
;;; EC-Eval value:
55

;;; EC-Eval input:
End of input stream reached.
Moriturus te saluto.
$ scheme
MIT/GNU Scheme running under MacOSX
Type `^C' (control-C) followed by `H' to obtain information about interrupts.

Copyright (C) 2011 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Image saved on Saturday October 26, 2013 at 11:02:50 PM
  Release 9.1.1 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/C 4.118
  Edwin 3.116

1 ]=> (load "./register5.14.scm")

;Loading "./register5.14.scm"... done
;Value: lookup-prim

1 ]=> 
(define (print a)
  (display a))

;Value: print

1 ]=> 
(define fib-machine
  (make-machine
   (list (list '< <) (list '- -) (list '+ +) (list 'print print)
         (list 'newline newline) (list 'read read))
   '(loop
      (assign continue (label fib-done))
      (initialize)
      (assign n (op read))
    fib-loop
      (test (op <) (reg n) (const 2))
      (branch (label immediate-answer))
      (save continue)
      (assign continue (label afterfib-n-1))
      (save n)
      (assign n (op -) (reg n) (const 1))
      (goto (label fib-loop))
    afterfib-n-1
      (restore n)
      (restore continue)
      (assign n (op -) (reg n) (const 2))
      (save continue)
      (assign continue (label afterfib-n-2))
      (save val)
      (goto (label fib-loop))
    afterfib-n-2
      (assign n (reg val))
      (restore val)
      (restore continue)
      (assign val
              (op +) (reg val) (reg n))
      (goto (reg continue))
    immediate-answer
      (assign val (reg n))
      (goto (reg continue))
    fib-done
       (perforrint-statistics)
       (perform (op newline))
       (goto (label loop)))))

;Value: fib-machine

1 ]=> (start fib-machine)
0
0
(total-pushes = 0 maximum-depth = 0)
1
1
(total-pushes = 0 maximum-depth = 0)
2
1
(total-pushes = 4 maximum-depth = 2)
3
2
(total-pushes = 8 maximum-depth = 4)
4
3
(total-pushes = 16 maximum-depth = 6)
5
5
(total-pushes = 28 maximum-depth = 8)
6
8
(total-pushes = 48 maximum-depth = 10)
7
13
(total-pushes = 80 maximum-depth = 12)
8
21
(total-pushes = 132 maximum-depth = 14)
9
34
(total-pushes = 216 maximum-depth = 16)
10
55
(total-pushes = 352 maximum-depth = 18)
End of input stream reached.
Moriturus te saluto.
$

Fibonacci数について。

翻訳版と解釈版について。

プッシュ回数とその比
n翻訳版解釈版
07160.4375
17160.4375
217720.2361
3271280.2109
4472400.1958
5774080.1887
61276880.1845
720711360.1822
833718640.1807
954730400.1799
1088749440.1794
最大スタック深さとその比
n翻訳版解釈版
0380.375
1380.375
25130.3846
38180.4444
411230.4783
514200.7
617330.5152
720380.5263
823430.5349
926480.5417
1029530.5472

特殊目的の計算機と解釈版について。

プッシュ回数とその比
n特殊目的解釈版
00160
10160
24720.0556
381280.0625
4162400.0667
5284080.0686
6486880.0698
78011360.0704
813218640.0708
921630400.0711
1035249440.0712
最大スタック深さとその比
n特殊目的解釈版
0080
1080
22130.1538
34180.2222
46230.2609
58200.4
610330.303
712380.3158
814430.3256
916480.3333
1018530.3396

特殊目的のコード対解釈されるコードの比と、翻訳するコード対解釈されるコードの比について。(n = 10の場合)

プッシュ回数とその比
特殊/解釈翻訳/解釈
0.07120.17940.3968
最大スタックの深さとその比
特殊/解釈翻訳/解釈
0.33960.54720.6206

特殊目的版が遥かに優れている。

0 コメント:

コメントを投稿