TCL - 如何知道某个功能已经运行了多长时间?

发布于 2024-10-20 17:58:21 字数 79 浏览 3 评论 0原文

假设我有一个 proc,该 proc 由多个语句和函数调用组成。我如何知道该函数到目前为止已经花费了多少时间?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不必你懂 2024-10-27 17:58:21

一个非常粗略的例子如下:

set TIME_start [clock clicks -milliseconds]
...do something...
set TIME_taken [expr [clock clicks -milliseconds] - $TIME_start]

使用时间过程,您可以执行以下操作:

% set tt [time {set x [expr 23 * 34]}]
38 microseconds per iteration

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
夜无邪 2024-10-27 17:58:21

要测量某些代码所花费的时间,您可以使用 timeclock

time 命令将运行其脚本参数并返回脚本花费多长时间的描述(以毫秒为单位)(加上一些描述性文本,使用 lindex 可以轻松删除) 。如果您确实在进行性能分析工作,您可以提供一个可选的计数参数,使脚本重复运行,但对于一般监控,您可以忽略它。

clock 命令可让您获取各种类型的时间戳(以及对时间进行格式化、解析和算术)。最粗略的是时钟秒,它返回自 Unix 纪元开始以来的时间量(以民用时间计算的秒数;这就是您想要的,除非您正在做一些专门的事情)。如果您需要更多详细信息,应使用时钟毫秒时钟微秒。还有时钟点击,但通常不会定义计数单位(除非您传递-milliseconds-microseconds 选项)。您可以将时间戳变成对您有用的东西。

如果您在 Tcl 8.4(或更早版本!)上计时,那么您只能使用时间时钟秒时钟点击(甚至连 -microseconds 选项也不存在;8.4 中也没有公开微秒分辨率的计时器)。在这种情况下,您应该考虑升级到 8.5,因为它通常速度更快。越快越好! (如果您使用的是 8.4 之前的版本,请务必升级,因为您在支持方面远远落后。)

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

几味少女 2024-10-27 17:58:21

要了解函数花费了多长时间,您可以使用 time 命令(包含函数调用)或使用 clock clicks 来获取之前和之后的当前时间的功能。 time 选项很简单,但只能为整个函数计时(并且只会在函数返回时为您提供时间)。使用时钟点击可以进行多次,但您需要自己从开始时间中减去当前时间。

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.

烟花易冷人易散 2024-10-27 17:58:21

如果您确实正在寻找某种类型的探查器,请查看 Tcllib 中的探查器包:
http://tcllib.sourceforge.net/doc/profiler.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文