TCL - 如何知道某个功能已经运行了多长时间?
假设我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个非常粗略的例子如下:
使用时间过程,您可以执行以下操作:
a very crude example would be something like:
Using the time proc, you can do the following:
要测量某些代码所花费的时间,您可以使用
time
或clock
。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
orclock
.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 withlindex
). 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 withclock 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 useclock milliseconds
orclock microseconds
. There's alsoclock 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
orclock 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.)要了解函数花费了多长时间,您可以使用
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 useclock clicks
to get the current time before and then during the function. Thetime
option is simple but can only time a whole function (and will only give you a time when the function returns). Usingclock clicks
can be done several times, but you will need to subtract the current time from the starting time yourself.如果您确实正在寻找某种类型的探查器,请查看 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