ZetCode

Tcl time Command

last modified April 3, 2025

The Tcl time command measures the execution time of a script. It's useful for performance testing and optimization. The command returns the time taken in microseconds per iteration.

Basic Definition

The time command executes a script multiple times and returns the average time per execution. It helps identify performance bottlenecks in code.

Syntax: time script ?count?. The script is executed count times (default 1). Returns microseconds per iteration as a string.

Basic Time Measurement

This example shows the simplest usage of time to measure a script.

basic_time.tcl
set result [time {set x [expr {100 * 200}]}]
puts "Execution time: $result"

This measures the time to multiply two numbers and store the result. The output shows microseconds per iteration and total iterations (1 by default).

Multiple Iterations

Using multiple iterations provides more accurate timing by averaging results.

time_iterations.tcl
set timing [time {
    for {set i 0} {$i < 1000} {incr i} {
        set x [expr {$i * $i}]
    }
} 100]
puts "Average time per iteration: $timing"

This runs a loop 1000 times, repeated 100 times for timing. The result shows the average time per full loop execution in microseconds.

Comparing Algorithms

time can compare different implementations of the same functionality.

time_compare.tcl
proc factorial_recursive {n} {
    if {$n <= 1} {return 1}
    expr {$n * [factorial_recursive [expr {$n - 1}]]}
}

proc factorial_iterative {n} {
    set result 1
    for {set i 2} {$i <= $n} {incr i} {
        set result [expr {$result * $i}]
    }
    return $result
}

puts "Recursive: [time {factorial_recursive 20} 1000]"
puts "Iterative: [time {factorial_iterative 20} 1000]"

This compares recursive vs iterative factorial implementations. The iterative version typically shows better performance due to less function call overhead.

String Operations Timing

time helps measure performance of string manipulation operations.

time_strings.tcl
set str "Tcl is a dynamic language"
set concat_time [time {
    for {set i 0} {$i < 1000} {incr i} {
        append str " with great features"
    }
} 10]

set replace_time [time {
    for {set i 0} {$i < 1000} {incr i} {
        string map {"dynamic" "powerful"} $str
    }
} 10]

puts "Concatenation: $concat_time"
puts "String replacement: $replace_time"

This measures string append vs string map operations. String manipulation performance is often important in text processing applications.

File Operations Timing

File I/O operations can be timed to identify potential bottlenecks.

time_files.tcl
set filename "testfile.txt"
set write_time [time {
    set fh [open $filename w]
    for {set i 0} {$i < 10000} {incr i} {
        puts $fh "Line $i"
    }
    close $fh
}]

set read_time [time {
    set fh [open $filename r]
    set content [read $fh]
    close $fh
}]

puts "Write time: $write_time"
puts "Read time: $read_time"
file delete $filename

This measures file writing and reading performance. File operations are often much slower than memory operations, making timing measurements valuable.

Procedure Timing

time can help optimize procedures by identifying slow sections.

time_proc.tcl
proc process_data {data} {
    set result {}
    foreach item $data {
        lappend result [expr {[string length $item] * 2}]
    }
    return $result
}

set test_data [lrepeat 1000 "sample"]
set proc_time [time {process_data $test_data} 100]

puts "Procedure execution time: $proc_time"

This measures the time to process a list of strings. The results help identify whether the procedure needs optimization for larger datasets.

Best Practices

This tutorial covered the Tcl time command with practical examples showing its usage for performance measurement and optimization.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Tcl Tutorials.