You are on page 1of 1

# stopwatch.

tcl - gives the CPU time consumed since the last stopwatch
# chrispy@synopsys.com
#
# v1.0 09/14/1999
# initial release
# v1.1 10/13/1999
# added support for memory
# v1.2 1/22/2004
# use namespaces to save previous cputime/mem values
# v1.3 2/10/2004
# fix harmless bug in namespaces

proc stopwatch {args} {


global sh_dev_null
parse_proc_arguments -args $args results

# namespaces let us save information across multiple calls to


# stopwatch, *and* ensure that someone else doesn't accidentally
# use the same variable name in another procedure
namespace eval stopwatch {}

# note we can check to see if this argument was passed or not by


# using "info exists"
if {[info exists results(info)]} {
set infomsg "(${results(info)})"
} else {
set infomsg ""
}

# when we run for the first time, just because we declared variables
# in our namespace above, doesn't mean that they actually exist yet;
# this is why we can use this check to know if it was the first time
# we've been invoked
if {[info exists stopwatch::last_cputime]} {
echo "Information: Time used since last stopwatch is [expr [cputime] -
$stopwatch::last_cputime]. $infomsg"
echo "Information: Memory used since last stopwatch is [expr [mem] -
$stopwatch::last_mem]k. $infomsg"
} else {
echo "Information: Time used since beginning of process is [cputime]. $infomsg"
echo "Information: Memory used since beginning of process is [mem]k. $infomsg"
}
redirect $sh_dev_null {set stopwatch::last_cputime [cputime]}
redirect $sh_dev_null {set stopwatch::last_mem [mem]}
}

define_proc_attributes stopwatch \
-info "gives CPU time/memory consumed since the last stopwatch" \
-define_args \
{
{ info "optional info message" "info" string optional }
}

You might also like