1 USING: kernel sequences namespaces make math assocs words arrays
2 tools.annotations vocabs sorting prettyprint io system
3 math.statistics accessors tools.time ;
9 : reset-word-timer ( -- )
10 H{ } clone *wordtimes* set-global
11 H{ } clone *calling* set-global ;
13 : lookup-word-time ( wordname -- utime n )
14 *wordtimes* get-global [ drop { 0 0 } ] cache first2 ;
16 : update-times ( utime current-utime current-numinvokes -- utime' invokes' )
17 rot [ + ] curry [ 1+ ] bi* ;
19 : register-time ( utime word -- )
21 [ lookup-word-time update-times 2array ] keep *wordtimes* get-global set-at ;
24 dup *calling* get-global set-at ; inline
26 : finished ( word -- )
27 *calling* get-global delete-at ; inline
29 : called-recursively? ( word -- t/f )
30 *calling* get-global at ; inline
32 : timed-call ( quot word -- )
33 [ calling ] [ [ benchmark ] dip register-time ] [ finished ] tri ; inline
35 : time-unless-recursing ( quot word -- )
36 dup called-recursively? not
37 [ timed-call ] [ drop call ] if ; inline
39 : (add-timer) ( word quot -- quot' )
40 [ swap time-unless-recursing ] 2curry ;
42 : add-timer ( word -- )
43 dup [ (add-timer) ] annotate ;
45 : add-timers ( vocab -- )
46 words [ add-timer ] each ;
48 : reset-vocab ( vocab -- )
49 words [ reset ] each ;
53 : time-dummy-word ( -- n )
54 [ 100000 [ [ dummy-word ] benchmark , ] times ] { } make median ;
56 : subtract-overhead ( {oldtime,n} overhead -- {newtime,n} )
58 swap [ * - ] keep 2array ;
60 : change-global ( variable quot -- )
61 global swap change-at ;
63 : (correct-for-timing-overhead) ( timingshash -- timingshash )
64 time-dummy-word [ subtract-overhead ] curry assoc-map ;
66 : correct-for-timing-overhead ( -- )
67 *wordtimes* [ (correct-for-timing-overhead) ] change-global ;
69 : print-word-timings ( -- )
70 *wordtimes* get-global [ swap suffix ] { } assoc>map natural-sort reverse pprint ;
72 : wordtimer-call ( quot -- )
75 correct-for-timing-overhead
78 print-word-timings nl ;
80 : profile-vocab ( vocab quot -- )
81 "annotating vocab..." print flush
82 over [ reset-vocab ] [ add-timers ] bi
84 "executing quotation..." print flush
86 "resetting annotations..." print flush
88 correct-for-timing-overhead