Version 2 of Profiling a TclOO class

Updated 2010-08-14 21:44:06 by tomk

I noticed the following example on comp.lang.tcl and thought that it was

  1. A good example of how to use the TclOO filter command
  2. A useful example in its own right

so I decided it was worth preserving on the wiki. tjk


 Fredrik wrote: 
 > sorry if this is a stupid question, but I would like to get into
 > profiling of some of my TclOO classes, i.e. some use cases for their
 > methods. Now, I see that the Tcllib profiler package redefines the
 > "proc" command to do the dynamic source code analysis. Now, would that
 > behave anywhere near correctly on a TclOO object method call?

 No.

 > If not, which I would guess is the case, are there alternatives which
 > would work?

 Mix in some kind of interceptor class into an object you want to
 watch, like this:
  oo::class create interceptor {
      filter INTERCEPT
      method INTERCEPT args {
          set t [time {
              catch {next {*}$args} msg opts
          }]
          puts "[lindex $t 0]ms for [lindex $args 0] on [self]"
          return -options $opts $msg
      }
  }

  # A silly example class
  oo::class create example {
      method bar x {
          for {set i 0} {$i<$x} {incr i} {
              incr out $i
          }
          return $out
      }
  }
  example create foo
  puts [foo bar 500]

  # Attach the instrumentation and rerun with a little profiling...
  oo::objdefine foo mixin interceptor
  puts [foo bar 500]
 OK, that's a very noddy example but it shows how to do it.
 Donal.