Performance in Tcl

  1. Performance Tools
  2. Performance Hints

Performance Tools

In a stock tcl binary, you can use the time command to measure the execution time of a command. The format of the command is: time tcl_command optional_repetition_count. For example:
% time {foreach i [list a b c d e ] { lappend result $i}} 
808 microseconds per iteration
% time {foreach i [list a b c d e ] { lappend result $i}}  10
259 microseconds per iteration
% time {foreach i [list a b c d e ] { lappend result $i}}  100
244 microseconds per iteration

The TclX package includes a profile command which can be used to return a call stack. TclX can be found at ftp://ftp.neosoft.com/pub/tcl/tclx-distrib.

Tycho uses profile to provide a Graphical Tcl Profiler.

Performance Hints

The hints below were taken from Practical Programming in Tcl and Tk, by Brent B. Welch, page 87.
  • Accessing large lists can really slow things down, using arrays is much faster. When you access an element in a list, the list must be reparsed, whereas arrays use a hash table, so access is nearly O(N).
  • Iterating through large data structures in Tcl is often slow. For each iteration, Tcl reparses the body of the loop or procedure. Brent Welch suggests the following hand tuning methods:
  • Inside a loop, shorter variable and procedure names can help. The tcl rename command can be useful in renaming long names to shorter names.
  • Moving large comment blocks outside of inner loops and frequently called procedure bodies can help. In the Tcl interpreter, comments are not removed, they are parsed each time the interpreter iterates through a loop or executes a procedure.
  • If you are using exec, then you may want to reconsider.
    1. The Tcl exec is not portable to non-unix platforms.
    2. There is a lot of overhead in starting another process. For example, calling the Unix date command is fairly slow:
      % time {exec date}
      287316 microseconds per iteration
      % time {puts [clock format [clock seconds]]}
      Sun Sep 22 16:10:33 PDT 1996
      1606 microseconds per iteration
      % 
      
      See the Tycho Exec Internals Guide.
  • Tycho Home Page

    Copyright © 1996, The Regents of the University of California. All rights reserved.
    Last updated: 96/09/03, comments to: eal@eecs.berkeley.edu