根据规范/实现,访问名称空间变量与upvar之间是否存在预期的差异。我必须使用回调函数。我不能只是传递一个argument.Empirically,upvar就赢了。但在所有合理的情况下,这都是意料之中的吗?谢谢。
发布于 2018-02-09 23:45:29
当然可以。完全作用域引用比upvar引用快,后者比variable引用快。
为了找出答案,命令'time‘是你的朋友:
namespace eval toto {
proc cb_upvar {varname} {
upvar $varname var
incr var
}
proc cb_scoped {varname} {
incr $varname
}
proc cb_variable {varname} {
variable $varname
incr $varname
}
}
proc benchmark {cmd} {
set toto::totovar 1
time $cmd 100
puts -nonewline "[lindex $cmd 0] =>\t"
puts [time $cmd 20000000]
}
puts [info tclversion]
benchmark {toto::cb_scoped ::toto::totovar}
benchmark {toto::cb_variable totovar}
benchmark {toto::cb_upvar totovar}输出:
toto::cb_scoped => 0.47478505 microseconds per iteration
toto::cb_variable => 0.7644891 microseconds per iteration
toto::cb_upvar => 0.6046395 microseconds per iterationRem:需要大量的迭代才能获得一致的结果。
https://stackoverflow.com/questions/48687557
复制相似问题