Article View: comp.lang.tcl
Article #129Re: Speed and Memory with upvar
From: 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'
Technical Details
Message-ID:
<yga33sw6avh.fsf@akutech.de>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11dn5cd$4ibt$1@tota-refugium.de>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Ralf Fassel <ralfixx@gmx.de> Newsgroups: comp.lang.tcl Subject: Re: Speed and Memory with upvar Date: Thu, 19 Nov 2026 17:31:46 +0100 Organization: A noiseless patient Spider Lines: 76 Message-ID: <yga33sw6avh.fsf@akutech.de> References: <11dn5cd$4ibt$1@tota-refugium.de> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Thu, 19 Nov 2026 16:31:47 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2537184"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+jOVeTkY2t/HPunomZuZJS"; posting-host="603cdb73cdc0d6a44183d1fe95d0d69f" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:FGidrpvu+w0Yq0lMYKeA0FxfUHw= sha1:Ni0K6UJB5IelOl8QSwNeu9VI2DI= sha256:Z2seBecqNxH06yKbVGGEv9GOIwRPDQ5oH5eoz81CQgg= sha1:ukx8wdjVd5JbIBOXFxV2GR9I/dY= sha256:4GeZIzqOJ4HPBgXir1Rj19HiI+zdWBdDLdVLuuq1bBc=