Article View: comp.lang.tcl
Article #137Re: Speed and Memory with upvar
From: 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
Technical Details
Message-ID:
<11dpj96$69cv$1@tota-refugium.de>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!reader5.news.weretis.net!news.tota-refugium.de!.POSTED!not-for-mailReferences:
<11dn5cd$4ibt$1@tota-refugium.de> <yga33sw6avh.fsf@akutech.de> <11dn9te$4mfp$1@tota-refugium.de> <ygay0an4vkg.fsf@akutech.de>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!reader5.news.weretis.net!news.tota-refugium.de!.POSTED!not-for-mail From: meshparts <alexandru.dadalau@meshparts.de> Newsgroups: comp.lang.tcl Subject: Re: Speed and Memory with upvar Date: Fri, 20 Nov 2026 14:51:27 +0100 Message-ID: <11dpj96$69cv$1@tota-refugium.de> References: <11dn5cd$4ibt$1@tota-refugium.de> <yga33sw6avh.fsf@akutech.de> <11dn9te$4mfp$1@tota-refugium.de> <ygay0an4vkg.fsf@akutech.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 20 Nov 2026 13:51:34 -0000 (UTC) Injection-Info: tota-refugium.de; logging-data="206239"; mail-complaints-to="abuse@news.tota-refugium.de" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:sXs9G940HmWlVVs5Nqaa8F+Z/KI= In-Reply-To: <ygay0an4vkg.fsf@akutech.de> X-User-ID: eJwNyMEBwEAEBMCWQqylnMPpv4RknoPXxZvmcMNiL2fcorrPIFSDwjiFJlYjzfPhX3eyWkTqAyEmEOc=