Version 0 of Profiling Tcl by Overloading Proc

Updated 2005-12-29 01:47:41

George Peter Staplin Dec 28, 2005 - I wanted the ability to profile my whole program without wrapping many commands with time, so I created a version of proc that logs the time.


 # By George Peter Staplin
 # Dec 2005
 proc debug_callback {args} {
  #
  # This opens the file in append mode every time it logs.  
  # For some uses it's more efficient not to do this.
  # I however found problems by keeping the file open when
  # using this code with other code that makes assumptions
  # about [file channels].
  #
  set fd [open $::debug_log_file a]
  puts $fd "debug_command: $args"
  close $fd
 }

 set ::debug_log_file debug.log
 rename proc _proc

 _proc proc {name arglist body} {
  set ns [uplevel 1 namespace current]
  set p [set ns]::[set name]
  _proc $p $arglist $body

  trace add execution $p enter debug_callback
  trace add execution $p leave debug_callback
 }

 proc debug_trace cmd {
  trace add execution $cmd enter debug_callback
  trace add execution $cmd leave debug_callback
 }

Possible future improvements might include keeping a log of the entire runtime, and then averaging the runtime by the number of calls. I however didn't need that for my usage, because I was killing the program with ^C. I also just realized that debug_trace could be used in _proc proc ... :)


Category Performance | Category Technique