in reply to Sheer idle curiousity . . .
why would using abs be so much faster
Because it requires less opcodes.
Best shown by the difference in the Concise output, where each successively faster option requires one less opcode than the precedent:
C:\test>perl -MO=Concise -E"my$x=-1; $x = $x<0?0:$x;" e <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 46 -e:1) v:%,{ ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV -1] s ->4 4 <0> padsv[$x:46,47] sRM*/LVINTRO ->5 6 <;> nextstate(main 47 -e:1) v:%,{ ->7 d <2> sassign vKS/2 ->e - <1> null sK/1 ->c a <|> cond_expr(other->b) sK/1 ->f 9 <2> lt sK/2 ->a 7 <0> padsv[$x:46,47] s ->8 8 <$> const[IV 0] s ->9 b <$> const[IV 0] s ->c f <0> padsv[$x:46,47] s ->c c <0> padsv[$x:46,47] sRM* ->d -e syntax OK C:\test>perl -MO=Concise -E"my$x=-1; $x = 0 unless $x >=0;" e <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 46 -e:1) v:%,{ ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV -1] s ->4 4 <0> padsv[$x:46,47] sRM*/LVINTRO ->5 6 <;> nextstate(main 47 -e:1) v:%,{ ->7 - <1> null vK/1 ->e a <|> or(other->b) vK/1 ->e 9 <2> ge sK/2 ->a 7 <0> padsv[$x:46,47] s ->8 8 <$> const[IV 0] s ->9 d <2> sassign vKS/2 ->e b <$> const[IV 0] s ->c c <0> padsv[$x:46,47] sRM* ->d -e syntax OK C:\test>perl -MO=Concise -E"my$x=-1; $x = 0 unless abs($x);" d <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 46 -e:1) v:%,{ ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV -1] s ->4 4 <0> padsv[$x:46,47] sRM*/LVINTRO ->5 6 <;> nextstate(main 47 -e:1) v:%,{ ->7 - <1> null vK/1 ->d 9 <|> or(other->a) vK/1 ->d 8 <1> abs[t3] sK/1 ->9 7 <0> padsv[$x:46,47] s ->8 c <2> sassign vKS/2 ->d a <$> const[IV 0] s ->b b <0> padsv[$x:46,47] sRM* ->c -e syntax OK
Of course, speed is no good to you if it doesn't perform the required function ... (davido++)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sheer idle curiousity . . .
by TheMonkWithoutVows (Novice) on Oct 23, 2013 at 22:20 UTC |