-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile.scm
72 lines (69 loc) · 2.83 KB
/
profile.scm
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(define (string.rpad s n c) (string s (string.rep c (- n (string.count s)))))
(define (string.rep s k)
(cond ((< k 4)
(cond ((<= k 0) "")
((= k 1) (string s))
((= k 2) (string s s))
(else (string s s s))))
((odd? k) (string s (string.rep s (- k 1))))
(else (string.rep (string s s) (/ k 2)))))
(let ((*profiles* (table))
(running #f))
(set! profile
(lambda (s)
(let ((f (top-level-value s)))
(put! *profiles* s (cons 0 (cons 0 0))) ; count, self, total
(set-top-level-value! s
(lambda args
(define tt (get *profiles* s))
(define last-tt running)
(define last-t0 (cddr tt))
(define t0 (time.now))
(set! running tt)
(define v (apply f args))
(set! running last-tt)
(define t1 (time.now))
(define tdelta (- t1 t0))
(if last-tt (set-car! (cdr last-tt) (- (cadr last-tt) tdelta)))
(set-car! (cdr tt) (+ (cadr tt) tdelta))
(set-cdr! (cdr tt) (+ last-t0 tdelta))
(set-car! tt (+ (car tt) 1))
v)))))
(set! show-profiles
(lambda ()
(define total 0)
(define pr (filter (lambda (x) (> (cadr x) 0))
(table.pairs *profiles*)))
(define width (+ 4
(apply max
(map (lambda (x)
(length (string x)))
(cons 'Function
(map car pr))))))
(princ (string.rpad "Function" width #\ )
"#Calls Total Time (seconds) Self Time (seconds)")
(newline)
(princ (string.rpad "--------" width #\ )
"------ -------------------- -------------------")
(newline)
(for-each
(lambda (p)
(set! total (+ total (cadr p)))
(princ (string.rpad (string (cadddr p)) width #\ )
(string.rpad (string (caddr p)) 11 #\ )
(string.rpad (string (car p)) 24 #\ )
(cadr p))
(newline))
(reverse (simple-sort (map (lambda (l) (reverse (to-proper l)))
pr))))
(princ (string.rpad "--------" width #\ )
"------ -------------------- -------------------")
(newline)
(princ (string.rpad "Total " width #\ )
" " (string total))
(newline)))
(set! clear-profiles
(lambda ()
(for-each (lambda (k)
(put! *profiles* k (cons 0 (cons 0 0))))
(table.keys *profiles*)))))