【问题标题】:How to measure cpu time of TCL script如何测量TCL脚本的cpu时间
【发布时间】:2016-09-27 11:05:41
【问题描述】:

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

我在 TCL 中尝试了 time 命令,但我不确定这是测量完整 TCL 脚本所花费的 cpu 时间的正确方法

【问题讨论】:

    标签: scripting tcl performance-testing cpu-time


    【解决方案1】:

    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 的调用会更容易。这对于粗略的测量来说已经足够好了。
    猜你喜欢
    • 2015-05-22
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 2013-06-09
    相关资源
    最近更新 更多