* meshparts <alex…@meshparts.de>
| I actually did a mistake in my original post: My experience is that
| the *second* procedure is faster than the first one, although is using
| upvar as an additional command. But I cannot explain this behaviour.
Most probably your procs modify the list, in which case (as Rich and
Harald already pointed out) a copy of the list will be created in
myproc1 (pass-by-shared-obj), whereas myproc2 (pass-by-name-reference)
operates directly on the list in the callers context (no copy required).
| So I suppose the memory consumption will be also identical in both
| cases.
Not if the list is modified in the proc:
- myproc1 needs to create a copy of the passed list to modifiy it
(shared Tcl_Obj*). A copy of the list requires creating [llength $l]
new Tcl_Obj*, copy the pointers and IncrRef for each pointer. That
naturally scales with the number of elements in the list.
- myproc2 operates directly on the list in the callers context,
non-shared Tcl_Obj*, no need to copy.
# two lists, short and long
% set l3 [list 1 2 3]
% set l1m [lrepeat 1000000 1]
# modify the list in the proc
% proc myproc1 {l} { lset l 2 y }
% proc myproc2 {lname} { upvar $lname l; lset l 2 x }
# proc1 short list
% time {myproc1 $l3} 100
1.23 microseconds per iteration
% time {myproc1 $l3} 100
1.22 microseconds per iteration
% time {myproc1 $l3} 100
1.23 microseconds per iteration
# proc1 long list
% time {myproc1 $l1m} 100
3670.52 microseconds per iteration
% time {myproc1 $l1m} 100
3217.89 microseconds per iteration
% time {myproc1 $l1m} 100
3418.59 microseconds per iteration
# proc2 short list
% time {myproc2 l3} 100
1.4 microseconds per iteration
% time {myproc2 l3} 100
1.35 microseconds per iteration
% time {myproc2 l3} 100
1.35 microseconds per iteration
# proc2 long list
% time {myproc2 l1m} 100
1.34 microseconds per iteration
% time {myproc2 l1m} 100
1.35 microseconds per iteration
% time {myproc2 l1m} 100
1.36 microseconds per iteration
HTH
R'
Re: Speed and Memory with upvar
- From
- Ralf Fassel <ralf…@gmx.de>
- Organization
- A noiseless patient Spider
- Message-ID
- <ygay…@akutech.de>
- Date
- Newsgroups
- comp.lang.tcl