summaryrefslogtreecommitdiffstats
path: root/bench.scm
blob: acb4a2cff447d7a447a8eab348c3d639c8390113 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

(require (in-vicinity (implementation-vicinity) "pi.scm"))
(require 'transcript)
(define isqrt
  (cond ((provided? 'inexact) sqrt)
	(else (require 'root) integer-sqrt)))
(define i/
  (cond ((provided? 'inexact) /)
	(else quotient)))
(define around
  (cond ((provided? 'inexact)
	 (lambda (x)
	   (cond ((>= 3000 (abs x) 3) (inexact->exact (round x)))
		 (else x))))
	(else identity)))

(define (time-pi digits)
  (let ((start-time (get-internal-run-time)))
    (pi digits 4)
    (i/ (* 1000 (- (get-internal-run-time) start-time))
       internal-time-units-per-second)))

(define (benchmark . arg)
  (define file
    (cond ((null? arg) "bench.log")
	  (else (car arg))))
  (do ((digits 50 (+ digits digits))
       (t 0 (time-pi (+ digits digits))))
      ((> t 3000)
       (do ((tl '() (cons (time-pi digits) tl))
	    (j 12 (+ -1 j)))
	   ((zero? j)
	    (let* ((avg (i/ (apply + tl) (length tl)))
		   (dev (isqrt (i/ (apply
				    + (map (lambda (x) (* (- x avg) (- x avg)))
						 tl))
				   (length tl)))))
	      (and file (transcript-on file))
	      (for-each display
			(list digits " digits took " (around avg) " mSec +/- "
			      (around dev) " mSec."))
	      (newline)
	      (let ((scaled-avg (i/ (* (i/ (* avg 1000) digits) 1000) digits)))
		(for-each display
			  (list " That is about " scaled-avg
				" mSec/k-digit^2 +/- "
				(around
				 (i/ (* 100 (i/ (* (i/ (* dev 1000) digits)
						   1000) digits))
				     scaled-avg))
				"%."))
		(newline)
		(and file (transcript-off)))
	      ))))))
(benchmark)