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

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

Thread Previous | Thread Next
May 20, 2015 02:39
Re: Curiosity about "do {;}" vs. "{;}" realtime cost
Message ID:
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

foo() if $bool;

is always faster than

if($bool) {

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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About