TCL - how to know how much time a function has worked?
Asked Answered
F

4

10

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?

Flexuous answered 9/3, 2011 at 7:30 Comment(0)
W
17

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
Washin answered 9/3, 2011 at 10:28 Comment(0)
C
5

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.)

Conterminous answered 9/3, 2011 at 10:7 Comment(0)
R
1

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.

Rociorock answered 9/3, 2011 at 7:34 Comment(5)
For example I run my TCL script and I want to measure the time elapsed. How I can do that?Flexuous
@Narek: I updated my answer for that; I also edited your question to clarify that you are asking about elapsed time, not future time.Rociorock
Please write an example with clock clicks to measure the time a function was running.Flexuous
I can't understand what does "clock clicks" returns. What is "high-resolution time value"? (See here: wiki.tcl.tk/4615 )Flexuous
@Narek: I would use clock clicks -milliseconds since that returns the time in known units.Rociorock
D
0

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

Decedent answered 10/3, 2011 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.