Scripting 如何测量TCL脚本的cpu时间

Scripting 如何测量TCL脚本的cpu时间,scripting,tcl,performance-testing,cpu-time,Scripting,Tcl,Performance Testing,Cpu Time,我有很长的TCL脚本。我想测量进程在TCL中占用的确切cpu时间 我在TCL中尝试了time命令,但我不确定它是否是测量完整TCL脚本占用的cpu时间的正确方法,因为TCL本身没有提供此信息。您希望使用TclX包的times命令 package require Tclx set pre [times] # do CPU intensive operation here; for example calculating the length of a number with many digit

我有很长的TCL脚本。我想测量进程在TCL中占用的确切cpu时间


我在TCL中尝试了time命令,但我不确定它是否是测量完整TCL脚本占用的cpu时间的正确方法,因为TCL本身没有提供此信息。您希望使用TclX包的
times
命令

package require Tclx

set pre [times]
# do CPU intensive operation here; for example calculating the length of a number with many digits...
string length [expr {13**12345}]
set post [times]

# the current process's non-OS CPU time is the first element; it's in milliseconds
set cpuSecondsTaken [expr {([lindex $post 0] - [lindex $pre 0]) / 1000.0}]
# The other elements are: system-cpu-time, subprocess-user-cpu-time, subprocess-system-time

当然,您依赖于操作系统正确地测量这些信息。它不能移植到非POSIX系统(例如Windows),尽管TWAPI包中可能有逻辑上类似的东西。

Tcl本身不提供此信息。您希望使用TclX包的
times
命令

package require Tclx

set pre [times]
# do CPU intensive operation here; for example calculating the length of a number with many digits...
string length [expr {13**12345}]
set post [times]

# the current process's non-OS CPU time is the first element; it's in milliseconds
set cpuSecondsTaken [expr {([lindex $post 0] - [lindex $pre 0]) / 1000.0}]
# The other elements are: system-cpu-time, subprocess-user-cpu-time, subprocess-system-time

当然,您依赖于操作系统正确地测量这些信息。它不可移植到非POSIX系统(例如Windows),尽管TWAPI包中可能有逻辑上类似的东西。

有时只需使用shell命令
time
来包装对Tcl的调用就更容易了。这对于粗略的测量来说已经足够好了。有时候,只需使用shell命令
time
来包装对Tcl的调用就更容易了。这对于粗略的测量来说已经足够好了。