-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench.zp
43 lines (38 loc) · 1.73 KB
/
bench.zp
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
(module "bench"
(export
(list :time time)
(list :bench bench))
(time (lambda (stmt)
"returns time <par>stmt</par> took to evaluate in nanoseconds.
params:
- stmt: the statement to evaluate
complexity: O(k) where k is the complexity of stmt
returns: the time in nanoseconds"
(begin
(define a (unix-timestamp))
(eval stmt)
(define b (unix-timestamp))
(+ (* (- (car b) (car a)) 1000000000) (- (cadr b) (cadr a))))))
(bench (lambda (stmt . count)
"evaluates the statements' performance and prints results;
optional argument: number of times this statement should be run.
params:
- stmt: the statement to evaluate
- count: the number of times the statement should be executed (optional)
complexity: O(k) where k is the complexity of stmt
returns: nil"
(let* ((counter (if (null? count) 100000 (car count)))
(nano-to-sec (lambda (x) (/. x 1000000000)))
(if-num-convert (lambda (x) (if (number? x) (number->string x) x)))
(evalfun (lambda (x) (time stmt)))
(evaluated (map evalfun (range counter)))
(total (reduce + 0 evaluated))
(average (/. total counter))
(minimum (list:min evaluated))
(maximum (list:max evaluated)))
(write (string:join (map if-num-convert
`("Tested function" ,counter "times:"
"\n\tAverage duration: " ,average "\t(" ,(/. average 1000000000) "secs)"
"\n\tBest case: " ,minimum "\t(" ,(nano-to-sec minimum) "secs)"
"\n\tWorst case: " ,maximum "\t(" ,(nano-to-sec maximum) "secs)"
"\n\tTotal: " ,total "\t(" ,(nano-to-sec total) "secs)")) #\space))))))