Making Timing More Easy

Created by CecilWesterhof.

When discussing the implementation of Split On Whitespace one of the factors was the performance. To make this easier I wrote the following proc:

proc doTime {command iterations tries {output True}} {
    if {![string is integer -strict $iterations] || \
            ![string is integer -strict $tries]} {
        error "iterations and tries should a integers ($iterations, $tries)"
    if {($iterations < 1) || ($tries < 1)} {
        error "iterations and tries should be at least 1 ($iterations, $tries)"
    set sigma 0

    for {set i 0} {$i < $tries} {incr i} {
        set   used [time $command $iterations]
        if {$output} {
            puts $used
        set sigma [expr {$sigma + [lindex $used 0]}]
    set average [expr {$sigma / $tries}]
    if {$output} {
        puts   [format "Average: %.2f" $average]
    return $average

It expects the command to be timed, for example:

    "splitOnWhiteSpace {$currentStr} -1 -1 True".

It furthermore expects iterations and tries. Optionally you can set the output.

Personally I think that you will use it most often interactively, so default the output is shown, but it can be disabled. Then it gives only back the average of the timings.

As always: comments, tips, questions and requests are appreciated.