Thread View: comp.lang.tcl
7 messages
7 total messages
Started by meshparts
Thu, 19 Nov 2026 15:42
Speed and Memory with upvar
Author: meshparts
Date: Thu, 19 Nov 2026 15:42
Date: Thu, 19 Nov 2026 15:42
23 lines
1419 bytes
1419 bytes
I'm thinking about the differences between two methods of passing a list
to a procedure:
First one is classical:
proc myproc1 {l} {}
set l [list 1 2 3]
myproc1 $l
Second one uses upvar:
proc myproc2 {lname} {upvar $lname l}
set l [list 1 2 3]
myproc2 l
My tests show that first one is faster than second one. At least for
lists of thousends of elements.
Does the second method also has advantages regarding the memory
consuption or are the two methods identical from this point of view?
Thanks
Alex
Re: Speed and Memory with upvar
Author: Ralf Fassel
Date: Thu, 19 Nov 2026 16:31
Date: Thu, 19 Nov 2026 16:31
76 lines
3305 bytes
3305 bytes
* meshparts <alexandru.dadalau@meshparts.de>
| I'm thinking about the differences between two methods of passing a
| list to a procedure:
>
| First one is classical:
>
| proc myproc1 {l} {}
| set l [list 1 2 3]
| myproc1 $l
>
| Second one uses upvar:
>
| proc myproc2 {lname} {upvar $lname l}
| set l [list 1 2 3]
| myproc2 l
>
| My tests show that first one is faster than second one.
Since the first proc simply does nothing, and the second actually calls
a command which requires some table-lookup to find the variable, a small
difference in favor of #1 is to be expected.
Keep in mind that only single Tcl_Obj-pointers are passed when calling
the procs, either to a Tcl_ListObj in myproc1, or to a Tcl_StringObj in
myproc2, so the calling overhead should be the same.
| At least for lists of thousends of elements.
Show the code you used for the test. I get identical times for a list
of 3 elements and a list of 1000000 elements.
% info patchlevel
8.6.18
% proc myproc1 {l} {}
% proc myproc2 {lname} {upvar $lname l}
% set l [list 1 2 3];
1 2 3
% time {myproc1 $l} 10000
0.9285 microseconds per iteration
% time {myproc1 $l} 10000
1.1559 microseconds per iteration
% time {myproc1 $l} 10000
0.9241 microseconds per iteration
% set l [lrepeat 1000000 1]; time {myproc1 $l} 10000
0.9497 microseconds per iteration
% time {myproc1 $l} 10000
0.9398 microseconds per iteration
% time {myproc1 $l} 10000
0.9357 microseconds per iteration
% time {myproc1 $l} 10000
0.9527 microseconds per iteration
% set l [list 1 2 3];
1 2 3
% time {myproc2 l} 10000
1.5462 microseconds per iteration
% time {myproc2 l} 10000
1.5442 microseconds per iteration
% time {myproc2 l} 10000
1.5622 microseconds per iteration
% set l [lrepeat 1000000 1]; time {myproc2 l} 10000
1.5281 microseconds per iteration
% time {myproc2 l} 10000
1.5158 microseconds per iteration
% time {myproc2 l} 10000
1.8271 microseconds per iteration
Plus I wouldn't put too big faith in calling times in the microsecond
region...
R'
Re: Speed and Memory with upvar
Author: meshparts
Date: Thu, 19 Nov 2026 16:59
Date: Thu, 19 Nov 2026 16:59
17 lines
1642 bytes
1642 bytes
Am 19.11.2026 um 17:31 schrieb Ralf Fassel: > e you used for the test. I get identical times for a list > of 3 elements and a list of 1000000 elements. Thank you Ralf. 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. Maybe it's a measuring error, since I could not test my long running procedures for many repeated runs. I only run the procedures a few times when testing. So I suppose the memory consumption will be also identical in both cases.
Re: Speed and Memory with upvar
Author: Rich
Date: Thu, 19 Nov 2026 17:59
Date: Thu, 19 Nov 2026 17:59
10 lines
1537 bytes
1537 bytes
meshparts <alexandru.dadalau@meshparts.de> wrote: > Does the second method also has advantages regarding the memory > consuption or are the two methods identical from this point of view? If the proc which receives the list does nothing to modify the list, both versions are identical, as only a reference is passed. But, if the proc changes the list, then some difference in memory usage will occur. How much will depend upon the nature of the change made to the list.
Re: Speed and Memory with upvar
Author: Harald Oehlmann
Date: Thu, 19 Nov 2026 18:05
Date: Thu, 19 Nov 2026 18:05
34 lines
2119 bytes
2119 bytes
Am 19.11.2026 um 16:42 schrieb meshparts:
> I'm thinking about the differences between two methods of passing a list
> to a procedure:
>
> First one is classical:
>
> proc myproc1 {l} {}
> set l [list 1 2 3]
> myproc1 $l
>
> Second one uses upvar:
>
> proc myproc2 {lname} {upvar $lname l}
> set l [list 1 2 3]
> myproc2 l
>
> My tests show that first one is faster than second one. At least for
> lists of thousends of elements.
>
> Does the second method also has advantages regarding the memory
> consuption or are the two methods identical from this point of view?
>
> Thanks
> Alex
I think the main difference is the intention:
In the first case, the proc may not modify list l in the caller space
In the second case, it is intentional, that callers variable l is modified.
So, the moment the list is modified by myproc1/2, solution 1 does create
a copy of the list.
Re: Speed and Memory with upvar
Author: Ralf Fassel
Date: Fri, 20 Nov 2026 10:59
Date: Fri, 20 Nov 2026 10:59
66 lines
3347 bytes
3347 bytes
* meshparts <alexandru.dadalau@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
Author: meshparts
Date: Fri, 20 Nov 2026 13:51
Date: Fri, 20 Nov 2026 13:51
71 lines
3566 bytes
3566 bytes
Am 20.11.2026 um 11:59 schrieb Ralf Fassel:
> * meshparts <alexandru.dadalau@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'
Many thanks all. Now I learned something very important to know.
Regards
Alex
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads