| 1 |
;;; |
| 2 |
;;; Some performance test for lazy sequences. |
| 3 |
;;; |
| 4 |
|
| 5 |
(use gauche.time) |
| 6 |
(use gauche.generator) |
| 7 |
(use gauche.lazy) |
| 8 |
(use file.util) |
| 9 |
(use scheme.list) |
| 10 |
(use srfi.42) |
| 11 |
|
| 12 |
;; Compare crating list of integers (0 ... N-1) with |
| 13 |
;; - simple loop |
| 14 |
;; - SRFI-42 list-ec |
| 15 |
;; - generator |
| 16 |
;; - lazy sequence |
| 17 |
;; - co-recursion using lcons |
| 18 |
(define (integer-sequence n) |
| 19 |
(define (simple n) |
| 20 |
(let loop ([k 0] [r '()]) |
| 21 |
(if (>= k n) |
| 22 |
(reverse r) |
| 23 |
(loop (+ k 1) (cons k r))))) |
| 24 |
(define (ec n) |
| 25 |
(list-ec (: k n) k)) |
| 26 |
(define (genr n) |
| 27 |
(let1 g (giota n) |
| 28 |
(let loop ([k (g)] [r '()]) |
| 29 |
(if (eof-object? k) |
| 30 |
(reverse r) |
| 31 |
(loop (g) (cons k r)))))) |
| 32 |
(define (laz n) |
| 33 |
(lrange 0 n)) |
| 34 |
(define (corec n) |
| 35 |
(define (rec k) |
| 36 |
(if (= k n) |
| 37 |
'() |
| 38 |
(lcons k (rec (+ k 1))))) |
| 39 |
(rec 0)) |
| 40 |
|
| 41 |
(time-these/report |
| 42 |
'(cpu 5) |
| 43 |
`((lazy-seq . ,(^[] (length (laz n)))) |
| 44 |
(co-recursion . ,(^[] (length (corec n)))) |
| 45 |
(generator . ,(^[] (length (genr n)))) |
| 46 |
(list-ec . ,(^[] (length (ec n)))) |
| 47 |
(simple-loop . ,(^[] (length (simple n)))))) |
| 48 |
) |
| 49 |
|
| 50 |
#| |
| 51 |
(integer-sequence 1000000) |
| 52 |
|# |
| 53 |
|
| 54 |
;; This compares eagerly created temporary list vs lazy seqeucne. |
| 55 |
;; Eagerly creating temp list supposedly stresses GC. |
| 56 |
|
| 57 |
(define (folding1 n) |
| 58 |
(define (eager n) |
| 59 |
(fold + 0 (iota n))) |
| 60 |
(define (lazy n) |
| 61 |
(fold + 0 (lrange 0 n))) |
| 62 |
(time-these/report |
| 63 |
'(cpu 10) |
| 64 |
`((lazy-seq . ,(^[] (lazy n))) |
| 65 |
(eager-iota . ,(^[] (eager n)))))) |
| 66 |
|
| 67 |
#| |
| 68 |
(folding1 10000000) |
| 69 |
|# |
| 70 |
|
| 71 |
;; Reading file |
| 72 |
|
| 73 |
(define (file f) |
| 74 |
(define (basic) |
| 75 |
(with-input-from-file f |
| 76 |
(^[] (let loop ([c (read-char)] [cnt 0]) |
| 77 |
(cond [(eof-object? c) cnt] |
| 78 |
[(char-whitespace? c) (loop (read-char) cnt)] |
| 79 |
[else (loop (read-char) (+ cnt 1))]))))) |
| 80 |
(define (eager) |
| 81 |
(call-with-input-file f |
| 82 |
(^p (count char-whitespace? (port->list read-char p))))) |
| 83 |
(define (lazy) |
| 84 |
(with-input-from-file f |
| 85 |
(cut count char-whitespace? (lseq read-char)))) |
| 86 |
|
| 87 |
(with-input-from-file f (cut generator-for-each identity read-char)) ;fill buffer |
| 88 |
(time-these/report |
| 89 |
'(cpu 5) |
| 90 |
`((lazy-seq . ,lazy) |
| 91 |
(eager . ,eager) |
| 92 |
(basic . ,basic)))) |
| 93 |
|
| 94 |
#| |
| 95 |
(file "./compile.c") |
| 96 |
|# |