1(import (edward util)
2 (chicken random)
3 (micro-benchmark))
4
5(define (repeat n thunk)
6 (when (> n 0)
7 (thunk)
8 (repeat (dec n) thunk)))
9
10(define (random-string . rest)
11 (let ((siz (if (null? rest) 100 (car rest))))
12 (list->string
13 (generate-list
14 (lambda ()
15 (let ((ascii (+ (pseudo-random-integer 126) 33)))
16 (integer->char ascii)))
17 siz))))
18
19(define (generate-list thunk size)
20 (map
21 (lambda (proc)
22 (proc))
23 (make-list size thunk)))
24
25(define (time-stat sym stats)
26 (define (msec->sec msec)
27 (/ msec 1000000))
28
29 (define (round-off z n)
30 (let ((power (expt 10 n)))
31 (/ (round (* power z)) power)))
32
33 (let ((el (assq sym stats)))
34 (if (not el)
35 (error (string-append "no element named '" (symbol->string sym) "' in statistics"))
36 (round-off (msec->sec (cdr el)) 3))))
37
38(define (run-benchmark name proc)
39 (let* ((stats (benchmark-run (BENCHMARK_ITERATIONS) (proc))))
40 (display name)
41 (display ":\t")
42 (display (time-stat 'arithmetic-mean stats))
43 (display "s")
44 (display " +/- ")
45 (display (time-stat 'standard-deviation stats))
46 (display "s")
47 (newline)))
48
49(define-syntax define-bench
50 (syntax-rules ()
51 ((define-bench (NAME) BODY ...)
52 (run-benchmark
53 (symbol->string (quote NAME))
54 (lambda ()
55 BODY ...)))))