lib.scm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;; -*- Scheme -*-
  2. ;;
  3. ;; A library of dumb functions that may be used to benchmark Guile-VM.
  4. ;; The comments are from Ludovic, a while ago. The speedups now are much
  5. ;; more significant (all over 2x, sometimes 8x).
  6. (define (fibo x)
  7. (if (or (= x 1) (= x 2))
  8. 1
  9. (+ (fibo (- x 1))
  10. (fibo (- x 2)))))
  11. (define (g-c-d x y)
  12. (if (= x y)
  13. x
  14. (if (< x y)
  15. (g-c-d x (- y x))
  16. (g-c-d (- x y) y))))
  17. (define (loop n)
  18. ;; This one shows that procedure calls are no faster than within the
  19. ;; interpreter: the VM yields no performance improvement.
  20. (if (= 0 n)
  21. 0
  22. (loop (1- n))))
  23. ;; Disassembly of `loop'
  24. ;;
  25. ;; Disassembly of #<objcode b79bdf28>:
  26. ;; nlocs = 0 nexts = 0
  27. ;; 0 (make-int8 64) ;; 64
  28. ;; 2 (load-symbol "guile-user") ;; guile-user
  29. ;; 14 (list 0 1) ;; 1 element
  30. ;; 17 (load-symbol "loop") ;; loop
  31. ;; 23 (link-later)
  32. ;; 24 (vector 0 1) ;; 1 element
  33. ;; 27 (make-int8 0) ;; 0
  34. ;; 29 (load-symbol "n") ;; n
  35. ;; 32 (make-false) ;; #f
  36. ;; 33 (make-int8 0) ;; 0
  37. ;; 35 (list 0 3) ;; 3 elements
  38. ;; 38 (list 0 2) ;; 2 elements
  39. ;; 41 (list 0 1) ;; 1 element
  40. ;; 44 (make-int8 5) ;; 5
  41. ;; 46 (make-false) ;; #f
  42. ;; 47 (cons)
  43. ;; 48 (make-int8 18) ;; 18
  44. ;; 50 (make-false) ;; #f
  45. ;; 51 (cons)
  46. ;; 52 (make-int8 20) ;; 20
  47. ;; 54 (make-false) ;; #f
  48. ;; 55 (cons)
  49. ;; 56 (list 0 4) ;; 4 elements
  50. ;; 59 (load-program ##{66}#)
  51. ;; 81 (define "loop")
  52. ;; 87 (variable-set)
  53. ;; 88 (void)
  54. ;; 89 (return)
  55. ;; Bytecode ##{66}#:
  56. ;; 0 (make-int8 0) ;; 0
  57. ;; 2 (local-ref 0)
  58. ;; 4 (ee?)
  59. ;; 5 (br-if-not 0 3) ;; -> 11
  60. ;; 8 (make-int8 0) ;; 0
  61. ;; 10 (return)
  62. ;; 11 (toplevel-ref 0)
  63. ;; 13 (local-ref 0)
  64. ;; 15 (make-int8 1) ;; 1
  65. ;; 17 (sub)
  66. ;; 18 (tail-call 1)
  67. (define (loopi n)
  68. ;; Same as `loop'.
  69. (let loopi ((n n))
  70. (if (= 0 n)
  71. 0
  72. (loopi (1- n)))))
  73. (define (do-loop n)
  74. ;; Same as `loop' using `do'.
  75. (do ((i n (1- i)))
  76. ((= 0 i))
  77. ;; do nothing
  78. ))
  79. (define (do-cons x)
  80. ;; This one shows that the built-in `cons' instruction yields a significant
  81. ;; improvement (speedup: 1.5).
  82. (let loop ((x x)
  83. (result '()))
  84. (if (<= x 0)
  85. result
  86. (loop (1- x) (cons x result)))))
  87. (define big-list (iota 500000))
  88. (define (copy-list lst)
  89. ;; Speedup: 5.9.
  90. (let loop ((lst lst)
  91. (result '()))
  92. (if (null? lst)
  93. result
  94. (loop (cdr lst)
  95. (cons (car lst) result)))))