Arc Forumnew | comments | leaders | submitlogin
A simple call-counting profiler
4 points by fallintothis 4661 days ago | discuss
It's been way too long since I coded anything for fun (new job keeps me busy), so I spent yesterday writing a simple call-counting profiler. Not that it hasn't been done before, and not like I'm likely to maintain it, but whatever.

Profile specific functions:

  arc> (profile map)
  Warning: map is being profiled; do not redefine it until you (unprofile map). 
  t
  arc> (map [apply + _] (pair '(1 3 5 7 9)))
  (4 12 9)
  arc> (profiles)

  Function | Call Count
  map      | 2

  nil
  arc> (unprofile map)
  nil
Profile all Arc builtins + any function defined in sig:

  arc> (profile-all)
  [...a lot of warnings...]
  t
  arc> (map [apply + _] (pair '(1 3 5 7 9)))
  (4 12 9)
  arc> (profiles)

  Function | Call Count
  is       | 25
  no       | 22
  cdr      | 20
  car      | 16
  cons     | 11
  copylist | 10
  apply    | 4
  list     | 4
  map1     | 4
  type     | 3
  +        | 3
  pair     | 3
  reclist  | 2
  isa      | 2
  cddr     | 2
  cadr     | 2
  testify  | 1
  alist    | 1
  map      | 1
  profiles | 1
  some     | 1

  nil
  arc> (unprofile-all)
  nil
Profiling a block of code:

  arc> (profiling (map [apply + _] (pair '(1 3 5 7 9))))

  Function | Call Count
  is       | 25
  no       | 22
  cdr      | 20
  car      | 16
  cons     | 11
  copylist | 10
  list     | 4
  apply    | 4
  map1     | 4
  type     | 3
  +        | 3
  pair     | 3
  reclist  | 2
  isa      | 2
  cddr     | 2
  cadr     | 2
  map      | 1
  testify  | 1
  alist    | 1
  some     | 1

  (4 12 9)
Profiling specific functions in a block of code:

  arc> (profiling-just cons (map [apply + _] (pair '(1 3 5 7 9))))

  Function | Call Count
  cons     | 11

  (4 12 9)
  arc> (profiling-just (cons pair) (map [apply + _] (pair '(1 3 5 7 9))))

  Function | Call Count
  cons     | 11
  pair     | 3

  (4 12 9)
Inline profiled functions:

  arc> (map (profiled [apply + _]) (pair '(1 3 5 7 9)))
  (4 12 9)
  arc> (profiles)

  Function             | Call Count
  (fn (_) (apply + _)) | 3

  nil
  arc> (map (profiled [+ _ 1]) '(1 2 3))
  (2 3 4)
  arc> (profiles)

  Function             | Call Count
  (fn (_) (apply + _)) | 3
  (fn (_) (+ _ 1))     | 3

  nil
Profiling specific spots in code, rather than functions:

  arc> (reset-profiles)
  ok
  arc> (def is-even (n)
         (if (profile-here test
               (is n 0))
             (profile-here base-case
               t)
             (profile-here recursive
               (no (is-even (- n 1))))))
  #<procedure: is-even>
  arc> (is-even 5)
  nil
  arc> (profiles)

  Function  | Call Count
  test      | 6
  recursive | 5
  base-case | 1

  nil
URL: https://bitbucket.org/fallintothis/profiler