develooper Front page | perl.perl5.porters | Postings from May 2015

Re: Curiosity about "do {;}" vs. "{;}" realtime cost

Thread Previous | Thread Next
From:
bulk88
Date:
May 20, 2015 02:39
Subject:
Re: Curiosity about "do {;}" vs. "{;}" realtime cost
Message ID:
BLU436-SMTP197886F04F68242E2C296B5DFC20@phx.gbl
Zefram wrote:
> Glenn Golden wrote:
>> I'd have guessed that both constructs would get optimized away during
>> compilation, but evidently "{;}" incurs some execution overhead that
>> "do {;}" does not.
> 
> A bare block is semantically treated as a loop that executes only once.
> It incurs the overhead of setting up the context frame that supports
> dynamic operators such as "next".  A do block, on the other hand, provides
> only scoping, so is much lighter at runtime.  There's also optimisation
> that applies only to do blocks: the dynamic scoping ops will be elided
> if not needed, as in this case.  I suppose in theory the empty bare
> block could be similarly optimised away, but the applicability would be
> more limited than what is done with do.  You can see the differences by
> applying "perl -MO=Concise".
> 
> -zefram

TLDR:
foo() if $bool;

is always faster than

if($bool) {
     foo();
}

long version:

Everything above, and also, the context frame must be setup to allow 
local and my vars to work, even if the block has none. AFAIK Perl does 
not do any optimizations on the fact that there are no eval 
STRINGs/local()/my declarations/next/redo/last in a block. Also "," has 
no overhead. Every ";" is an op that executes that adjusts current line 
number and IIRC flushes the mortal stack (which is most visible in PP by 
DESTROY methods running after assignment wipes the last ref to an obj), 
but I think if you put 2 ";"s on 1 line, both execute, even though the 
line number isn't advancing. There is alot of stuff that could be done, 
that every other interpreted language that competes with Perl does, but 
politics and to a lesser extent technical skill prevent it.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About