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++)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Sheer idle curiousity . . . by BrowserUk
in thread Sheer idle curiousity . . . by TheMonkWithoutVows

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.