aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/common/lib/profiler.frt
blob: d2515e73e1562bd022ad8216434de018915bdbf3 (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
\ A profiler counts the number of calls
\ of any word being defined afterwards.
\
\ global state: on and off
variable profiling?
: profile:on -1 profiling? ! ;
: profile:off 0 profiling? ! ;

: profiler profiling? @ if 1 swap +! else drop then ; 
\ re-define colon
: : :
  here 2 allot postpone literal postpone profiler
;

\ get the address of the profiling data.
: xt>prf ( xt -- addr )
  cell+ @i
;

\ useful stuff
: .prf xt>prf @ u. ;
: prf-reset xt>prf 0 swap ! ;

\ usage
\   : foo bar baz ;
\   profile:on      -- turn on profiling
\   ' foo .prf      -- gets the number of calls to foo
\   ' foo prf-reset -- resets this number
\   profile:off     -- turn off profiling
\ wanna profile system words? just re-define them now ;)
\   e.g. : + + ;