Say I have a proc and the proc consists of several statements and function calls. How I can know how much time the function has taken so far?
a very crude example would be something like:
set TIME_start [clock clicks -milliseconds]
...do something...
set TIME_taken [expr [clock clicks -milliseconds] - $TIME_start]
Using the time proc, you can do the following:
% set tt [time {set x [expr 23 * 34]}]
38 microseconds per iteration
To measure the time some code has taken, you either use time
or clock
.
The time
command will run its script argument and return a description of how long the script took, in milliseconds (plus some descriptive text, which is trivial to chop off with lindex
). If you're really doing performance analysis work, you can supply an optional count argument that makes the script be run repeatedly, but for just general monitoring you can ignore that.
The clock
command lets you get various sorts of timestamps (as well as doing formatting, parsing and arithmetic with times). The coarsest is got with clock seconds
, which returns the amount of time since the beginning of the Unix epoch (in seconds computed with civil time; that's what you want unless you're doing something specialized). If you need more detail, you should use clock milliseconds
or clock microseconds
. There's also clock clicks
, but it's not typically defined what unit that's counting in (unless you pass the -milliseconds
or -microseconds
option). It's up to you to turn the timestamps into something useful to you.
If you're timing things on Tcl 8.4 (or before!) then you're constrained to using time
, clock seconds
or clock clicks
(and even the -microseconds
option is absent; there's no microsecond-resolution timer exposed in 8.4). In that case, you should consider upgrading to 8.5, as it's generally faster. Faster is Good! (If you're using pre-8.4, definitely upgrade as you're enormously behind on the support front.)
To tell how long a function has taken, you can either use the time
command (wrapped around the function call) or use clock clicks
to get the current time before and then during the function. The time
option is simple but can only time a whole function (and will only give you a time when the function returns). Using clock clicks
can be done several times, but you will need to subtract the current time from the starting time yourself.
clock clicks -milliseconds
since that returns the time in known units. –
Rociorock In case your really looking for some kind of profiler, have a look at the profiler package in Tcllib: http://tcllib.sourceforge.net/doc/profiler.html
© 2022 - 2024 — McMap. All rights reserved.