in reply to Re: Benchmarking for loops?
in thread Benchmarking for loops?

What do you see? I see only one opcode difference (an extra nextstate). Which perl version?

Replies are listed 'Best First'.
Re^3: Benchmarking for loops?
by chromatic (Archbishop) on Jul 13, 2004 at 05:48 UTC

    Here's a diff. This is a vanilla 5.8.3 on Linux PPC.

    --- postfix 2004-07-12 22:45:53.000000000 -0700 +++ block 2004-07-12 22:45:44.000000000 -0700 @@ -3,23 +3,23 @@ 2 <;> nextstate(main 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const(IV 0) s ->4 -4 <0> padsv[$total:1,2] sRM*/LVINTRO ->5 -6 <;> nextstate(main 2 -e:1) v ->7 -7 <;> nextstate(main 2 -e:1) v ->8 +4 <0> padsv[$total:1,4] sRM*/LVINTRO ->5 +6 <;> nextstate(main 3 -e:1) v ->7 j <2> leaveloop vK/2 ->k -c <{> enteriter(next->g last->j redo->d) lKS ->h -- <0> ex-pushmark s ->8 -- <1> ex-list lK ->b -8 <0> pushmark s ->9 -9 <$> const(IV 1) s ->a -a <$> const(NV 1000000) s ->b -b <$> gv(*_) s ->c +b <{> enteriter(next->g last->j redo->c) lKS ->h +- <0> ex-pushmark s ->7 +- <1> ex-list lK ->a +7 <0> pushmark s ->8 +8 <$> const(IV 1) s ->9 +9 <$> const(NV 1000000) s ->a +a <$> gv(*_) s ->b - <1> null vK/1 ->j -i <|> and(other->d) vK/1 ->j +i <|> and(other->c) vK/1 ->j h <0> iter s ->i - <@> lineseq vK ->- -f <2> add[t2] vKS/2 ->g -d <0> padsv[$total:1,2] sRM ->e +c <;> nextstate(main 2 -e:1) v ->d +f <2> add[t4] vKS/2 ->g +d <0> padsv[$total:1,4] sRM ->e - <1> ex-rv2sv sK/1 ->f e <$> gvsv(*_) s ->f g <0> unstack v ->h
      Thanks, somehow I missed that first nextstate. It's still there in bleadperl, and seems totally unnecessary to me.
Re^3: Benchmarking for loops?
by stvn (Monsignor) on Jul 13, 2004 at 03:02 UTC

    I believe (but do not know for absolutely sure) that nextstate is the opcode that sets things up to enter a block or maybe it is exit the block, I can't remember for sure (something is making me think that the op-tree is traversed/processed in post-order rather than pre-order and that might make the difference). If you are really interested you might want to check out Simon Cozens Perl 5 Internals paper, in particular the end part talks about some of the output of the B modules, and how to read/understand them.

    The (overly simplistic) reason why entering an exiting a block has a performance penalty is that perl is lexically scoped. So when entering each block a new lexical scratchpad needs to be set up, and then upon exiting the block all variables need to be restored.

    -stvn
      nextstate preps things between statements. It sets a pointer used to give warn and die messages a line number, clears the tainted-expression flag, resets the stack pointer, and frees any temporary variables generated by the previous statement.

      scoping is done mostly by scope (for a scope containing a single statement), or enter* & leave (for a scope containing multiple statements).