TCL中的递归过程调用

时间:2015-10-05 14:27:17

标签: performance recursion tcl

我有一个tcl环境,由许多tcl文件和程序组成,所有这些文件都在同一个命名空间下。开发了一个程序,以便根据输入参数递归地调用自身。 synopsys是:

RemoteViews

其中:

  • subcmd 是子命令
  • arg1 是一个寄存器

arg1可能是普通寄存器或所有寄存器,导致将 subcmd 子命令提交给允许的整体注册人。 reporting子命令是 get_status / get_delta / get_mask ,最坏的情况由 get_value 子命令表示,该子命令连续调用前一个子命令。如果提供了 all 寄存器,则过程级别堆栈如下:

myproc *subcmd arg1*

这样我就可以慢慢执行最糟糕的执行。 我尝试在互联网上浏览以获取有关如何在没有结果的情况下提高性能的信息。一个可能的解决方案似乎是 apply 命令(lambda函数的父母),但我想这对我的目的来说太简单了,其他方法可能满足我的问题。有什么建议吗?

目前我用内在声明回忆了程序本身:

myproc get_value all
   myproc get_value reg1
      myproc get_status reg1
      myproc get_delta reg1
      myproc get_mask reg1
   myproc get_value reg2
      myproc get_status reg2
      myproc get_delta reg2
      myproc get_mask reg2
   ...
   myproc get_value reg(n-1)
      myproc get_status reg(n-1)
      myproc get_delta reg(n-1)
      myproc get_mask reg(n-1)
   myproc get_value reg(n)
      myproc get_status reg(n)
      myproc get_delta reg(n)
      myproc get_mask reg(n)

编辑日志和其他信息

我修改了代码,以便跟踪忠实于真实的代码。此外,我发布了一些源代码片段,以便您可以看到如何有效地调用这些过程:

eval {myproc *subcmd* arg1}

后续get_ *驻留的行如下(加上清理寄存器的clear_delta):

# this is the call statement for 'all' register.
# exp_opt1_list holds reg1, reg2, ..., reg(n)
# the other parameter are not worth to explain, they are parsed above
# *callprocname* is a procedure that return back the name of the call procedure which reside 1 level above. Practically this procedure is shared by identical procedures at upper level which differ only for the lists of registers. So callprocname gives the name of the upper procedure that originally called that core procedure
foreach opt1_scan $exp_opt1_list($sub_cmd) {
    if {($opt1  == "all") && $opt1_scan != "all" } {
       set scan_result_list [string tolower [callprocname]] $sub_cmd $tag1 $tag2 $tag3 $opt1_scan $local_verbose -loop]
       [...]
    }
}

作为第一个代码主干状态中的注释,有许多嵌套过程,尽管它们中的大部分都是微不足道的,或者换句话说它们的计算要求不高。事实上,结构涉及很多层次,让我们说6个层次(服务+核心),因为一切都从调用核心的服务级别开始,如果涉及所有寄存器直接从核心再次调用服务级别,因此我们有服务 - >核心 - >服务 - >核心等。

这个粗略的解释是否足够?

1 个答案:

答案 0 :(得分:0)

你几乎猜到我的代码是如何工作的,但我修改它以使它更接近我的代码。

你的代码看起来像这样吗?

proc myproc {subcmd reg} {
    set n 5
    switch -- $subcmd {
        get_value {
            if {$reg eq "all"} {
               for {set i 1} {$i < $n} {incr i} {
                    myproc get_status reg$i
                    myproc get_delta reg$i
                    myproc get_mask reg$i
               }
            }
        }
        get_status {
            if {$reg eq "all"} {
                for {set i 1} {$i < $n} {incr i} {
                    myproc get_status reg$i
                }
            } else {
                # do what get_status does with a single register
            }
        }
        get_delta {
            if {$reg eq "all"} {
                for {set i 1} {$i < $n} {incr i} {
                    myproc get_delta reg$i
                }
            } else {
                # do what get_delta does with a single register
            }
        }
        get_mask {
            if {$reg eq "all"} {
                for {set i 1} {$i < $n} {incr i} {
                    myproc get_mask reg$i
                }
            } else {
                # do what get_mask does with a single register
            }
        }
    }
}

我问,因为我们确实需要知道您的代码能够为您提供建议。

上述代码的呼叫追踪:

myproc get_value all
    myproc get_status reg1
    myproc get_delta reg1
    myproc get_mask reg1
    myproc get_status reg2
    myproc get_delta reg2
    myproc get_mask reg2
    myproc get_status reg3
    myproc get_delta reg3
    myproc get_mask reg3
    myproc get_status reg4
    myproc get_delta reg4
    myproc get_mask reg4

BTW我已经尝试过&#34; profile&#34;我的代码[时间],似乎它的缓慢归因于嵌套调用......