OUTPUT BUFFER:
#----- begin package profiler package require Tcl 8.5 namespace eval profiler {variable T; variable S} proc profiler::enter args { variable S ::profiler::push S [clock microseconds] } proc profiler::leave {str args} { variable T; variable S ::profiler::push T([lindex $str 0]) [expr {-[::profiler::pop S] + [clock microseconds]}] } proc profiler::do {what cmdlist} { set cmd "" foreach i $cmdlist { if {!([string match *profiler::* $i])} { append cmd "trace $what execution $i enter ::profiler::enter\n" append cmd "trace $what execution $i leave ::profiler::leave\n" } } eval $cmd } interp alias {} profiler::push {} lappend proc profiler::pop _stack { upvar 1 $_stack stack set res [lindex $stack end] set stack [lrange $stack 0 end-1] return $res } proc profiler::stats list { set sum [expr double([join $list +])] format "%10.0f %6d %10.2f" $sum [llength $list] [expr {$sum/[llength $list]}] } proc profiler::report {{channel stdout}} { variable T foreach i [lsort -dic [array names T]] { puts $channel [format "%-20s %s" $i [stats $T($i)]] } } #----- end package profiler proc main argv { foreach i {1 2 3 4} {puts ==[f $i]} } proc f x {expr {$x*$x}} #-- We activate profiling on all procs, and a few selected Tcl commands: #profiler::do add [concat [info procs] {expr lindex foreach upvar puts clock}] profiler::do add [info procs] main $argv profiler::do remove [info commands] ;# no error if more removed than added profiler::report