Let's say I have a variable which is one level up, which I just want to query its' value. I have two options:
uplevel { set var_name }
Or:
upvar var_name
If I need to query the variable just once, and not change it, which one should be faster?
Tcl upvar and uplevel in performance
359 Views Asked by user1134991 At
2
There are 2 best solutions below
0
On
I prefer timerate for this kind of micro-benchmarking:
% namespace import ::tcl::unsupported::timerate
% timerate -calibrate {}
0.03257451263357219 µs/#-overhead 0.032807 µs/# 59499506 # 30481304 #/sec
% proc foo {y} {set x 1; bar $y}
% proc bar {y} {upvar 1 x x; list $x $y}
% timerate {foo 2} 10000
0.437240 µs/# 21285016 # 2287075 #/sec 9306.651 net-ms
% proc bar {y} {set x [uplevel 1 {set x}]; list $x $y}
% timerate {foo 2} 10000
0.612693 µs/# 15497439 # 1632137 #/sec 9495.179 net-ms
(Answer holds, clearly: Use upvar).
You'll find that
upvaris probably faster for this. Not necessarily, but most likely. (If you're worried about performance,timethe alternatives.) Note that they will both necessarily have to resolve the variable name; that's a cost that's going to be borne anyway. But the version withupvardoesn't involve moving code between contexts, so it is likely to be faster.FWIW, when I try with the example below, my intuition is correct. (The key is that one uses the
upvarbytecode opcode; the other doesinvokeStk, which is slower because that's the general command dispatcher and has a bunch of overhead necessary for other purposes.)