| 1 |
; Hacked to estimate cost of one closed call per character. |
| 2 |
|
| 3 |
(define transcoder 'usual) |
| 4 |
|
| 5 |
(define (slow-read-char in) |
| 6 |
(if (eq? transcoder 'usual) |
| 7 |
(read-char in) |
| 8 |
(read-char (car (vector->list (make-vector 3 in)))))) |
| 9 |
|
| 10 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 11 |
; |
| 12 |
; Reading benchmarks. |
| 13 |
; |
| 14 |
; The timed portion of read-characters-from-file-port-benchmark |
| 15 |
; uses read-char to read nboyer.sch 1000 times, performing |
| 16 |
; file i/o each time. |
| 17 |
; |
| 18 |
; The timed portion of read-from-file-port-benchmark |
| 19 |
; parses nboyer.sch 1000 times, performing file i/o |
| 20 |
; each time. |
| 21 |
; |
| 22 |
; The timed portion of read-from-string-port-benchmark |
| 23 |
; parses the string representation of nboyer.sch 1000 times. |
| 24 |
; |
| 25 |
; The output of that parse is checked by comparing it |
| 26 |
; the the value returned by the read procedure. |
| 27 |
; |
| 28 |
; Usage: |
| 29 |
; (read-from-file-port-benchmark n input) |
| 30 |
; (read-from-string-port-benchmark n input) |
| 31 |
; |
| 32 |
; n defaults to 1000, and input defaults to "nboyer.sch". |
| 33 |
; |
| 34 |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
| 35 |
|
| 36 |
(define (read-characters-from-file-benchmark . rest) |
| 37 |
(let* ((n (if (null? rest) 1000 (car rest))) |
| 38 |
(input (if (or (null? rest) (null? (cdr rest))) |
| 39 |
"nboyer.sch" |
| 40 |
(cadr rest))) |
| 41 |
(benchmark-name |
| 42 |
(string-append "chario:file:" input ":" (number->string n)))) |
| 43 |
(run-benchmark benchmark-name |
| 44 |
n |
| 45 |
(lambda () |
| 46 |
(call-with-input-file |
| 47 |
input |
| 48 |
(lambda (in) |
| 49 |
(do ((x (slow-read-char in) (slow-read-char in)) |
| 50 |
(n 0 (+ n 1))) |
| 51 |
((eof-object? x) n))))) |
| 52 |
(lambda (x) #t)))) |
| 53 |
|
| 54 |
(define (read-characters-from-string-benchmark . rest) |
| 55 |
(let* ((n (if (null? rest) 1000 (car rest))) |
| 56 |
(input (if (or (null? rest) (null? (cdr rest))) |
| 57 |
"nboyer.sch" |
| 58 |
(cadr rest))) |
| 59 |
(input-string (read-file-as-string input)) |
| 60 |
(benchmark-name |
| 61 |
(string-append "chario:string:" input ":" (number->string n)))) |
| 62 |
(run-benchmark benchmark-name |
| 63 |
n |
| 64 |
(lambda () |
| 65 |
(let ((in (open-input-string input-string))) |
| 66 |
(do ((x (slow-read-char in) (slow-read-char in)) |
| 67 |
(n 0 (+ n 1))) |
| 68 |
((eof-object? x) n)))) |
| 69 |
(lambda (x) #t)))) |
| 70 |
|
| 71 |
(define (read-from-file-benchmark . rest) |
| 72 |
(let* ((n (if (null? rest) 1000 (car rest))) |
| 73 |
(input (if (or (null? rest) (null? (cdr rest))) |
| 74 |
"nboyer.sch" |
| 75 |
(cadr rest))) |
| 76 |
(answer (call-with-input-file |
| 77 |
input |
| 78 |
(lambda (in) |
| 79 |
(do ((x (read in) (read in)) |
| 80 |
(answer '() x)) |
| 81 |
((eof-object? x) |
| 82 |
answer))))) |
| 83 |
(benchmark-name |
| 84 |
(string-append "reading:file:" input ":" (number->string n)))) |
| 85 |
(run-benchmark benchmark-name |
| 86 |
n |
| 87 |
(lambda () |
| 88 |
(call-with-input-file |
| 89 |
input |
| 90 |
(lambda (in) |
| 91 |
(do ((x (read in) (read in)) |
| 92 |
(y #f x)) |
| 93 |
((eof-object? x) y))))) |
| 94 |
(lambda (x) (equal? x answer))))) |
| 95 |
|
| 96 |
(define (read-from-string-benchmark . rest) |
| 97 |
(let* ((n (if (null? rest) 1000 (car rest))) |
| 98 |
(input (if (or (null? rest) (null? (cdr rest))) |
| 99 |
"nboyer.sch" |
| 100 |
(cadr rest))) |
| 101 |
(input-string (read-file-as-string input)) |
| 102 |
(answer (call-with-input-file |
| 103 |
input |
| 104 |
(lambda (in) |
| 105 |
(do ((x (read in) (read in)) |
| 106 |
(answer '() x)) |
| 107 |
((eof-object? x) |
| 108 |
answer))))) |
| 109 |
(benchmark-name |
| 110 |
(string-append "reading:string:" input ":" (number->string n)))) |
| 111 |
(run-benchmark benchmark-name |
| 112 |
n |
| 113 |
(lambda () |
| 114 |
(let ((in (open-input-string input-string))) |
| 115 |
(do ((x (read in) (read in)) |
| 116 |
(y #f x)) |
| 117 |
((eof-object? x) y)))) |
| 118 |
(lambda (x) (equal? x answer))))) |
| 119 |
|
| 120 |
(define (read-file-as-string name) |
| 121 |
(call-with-input-file |
| 122 |
name |
| 123 |
(lambda (in) |
| 124 |
(do ((x (read-char in) (read-char in)) |
| 125 |
(chars '() (cons x chars))) |
| 126 |
((eof-object? x) |
| 127 |
(list->string (reverse chars))))))) |
| 128 |
|
| 129 |
(define (main . args) |
| 130 |
(run-benchmark |
| 131 |
"slow-read-chars-from-string" |
| 132 |
1 |
| 133 |
(lambda () |
| 134 |
(read-characters-from-string-benchmark reading-iters)) |
| 135 |
(lambda (result) #t))) |