cleanups in syntax.scm
[bpt/guile.git] / benchmark / lib.scm
1 ;; -*- Scheme -*-
2 ;;
3 ;; A library of dumb functions that may be used to benchmark Guile-VM.
4
5
6 (define (fibo x)
7 (if (or (= x 1) (= x 2))
8 1
9 (+ (fibo (- x 1))
10 (fibo (- x 2)))))
11
12 (define (g-c-d x y)
13 (if (= x y)
14 x
15 (if (< x y)
16 (g-c-d x (- y x))
17 (g-c-d (- x y) y))))
18
19 (define (loop n)
20 ;; This one shows that procedure calls are no faster than within the
21 ;; interpreter: the VM yields no performance improvement.
22 (if (= 0 n)
23 0
24 (loop (1- n))))
25
26 ;; Disassembly of `loop'
27 ;;
28 ; Disassembly of #<objcode 302360b0>:
29
30 ; nlocs = 0 nexts = 0
31
32 ; 0 (make-int8 64) ;; 64
33 ; 2 (link "=")
34 ; 5 (link "loop")
35 ; 11 (link "1-")
36 ; 15 (vector 3)
37 ; 17 (make-int8:0) ;; 0
38 ; 18 (load-symbol "n") ;; n
39 ; 28 (make-false) ;; #f
40 ; 29 (make-int8:0) ;; 0
41 ; 30 (list 3)
42 ; 32 (list 2)
43 ; 34 (list 1)
44 ; 36 (make-int8 8) ;; 8
45 ; 38 (make-int8 2) ;; 2
46 ; 40 (make-int8 6) ;; 6
47 ; 42 (cons)
48 ; 43 (cons)
49 ; 44 (make-int8 23) ;; 23
50 ; 46 (make-int8 4) ;; 4
51 ; 48 (make-int8 12) ;; 12
52 ; 50 (cons)
53 ; 51 (cons)
54 ; 52 (make-int8 25) ;; 25
55 ; 54 (make-int8 4) ;; 4
56 ; 56 (make-int8 6) ;; 6
57 ; 42 (cons)
58 ; 43 (cons)
59 ; 44 (make-int8 23) ;; 23
60 ; 46 (make-int8 4) ;; 4
61 ; 48 (make-int8 12) ;; 12
62 ; 50 (cons)
63 ; 51 (cons)
64 ; 52 (make-int8 25) ;; 25
65 ; 54 (make-int8 4) ;; 4
66 ; 56 (make-int8 6) ;; 6
67 ; 58 (cons)
68 ; 59 (cons)
69 ; 60 (list 4)
70 ; 62 load-program ##{201}#
71 ; 89 (link "loop")
72 ; 95 (variable-set)
73 ; 96 (void)
74 ; 97 (return)
75
76 ; Bytecode ##{201}#:
77
78 ; 0 (object-ref 0)
79 ; 2 (variable-ref)
80 ; 3 (make-int8:0) ;; 0
81 ; 4 (local-ref 0)
82 ; 6 (call 2)
83 ; 8 (br-if-not 0 2) ;; -> 13
84 ; 11 (make-int8:0) ;; 0
85 ; 12 (return)
86 ; 13 (object-ref 1)
87 ; 15 (variable-ref)
88 ; 16 (object-ref 2)
89 ; 18 (variable-ref)
90 ; 19 (local-ref 0)
91 ; 21 (call 1)
92 ; 23 (tail-call 1)
93
94
95 (define (loopi n)
96 ;; Same as `loop'.
97 (let loopi ((n n))
98 (if (= 0 n)
99 0
100 (loopi (1- n)))))
101
102 (define (do-loop n)
103 ;; Same as `loop' using `do'.
104 (do ((i n (1- i)))
105 ((= 0 i))
106 ;; do nothing
107 ))
108
109
110 (define (do-cons x)
111 ;; This one shows that the built-in `cons' instruction yields a significant
112 ;; improvement (speedup: 1.5).
113 (let loop ((x x)
114 (result '()))
115 (if (<= x 0)
116 result
117 (loop (1- x) (cons x result)))))
118
119 (define big-list (iota 500000))
120
121 (define (copy-list lst)
122 ;; Speedup: 5.9.
123 (let loop ((lst lst)
124 (result '()))
125 (if (null? lst)
126 result
127 (loop (cdr lst)
128 (cons (car lst) result)))))