Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: Newbie question

by Marshall (Canon)
on Aug 23, 2022 at 11:44 UTC ( [id://11146309]=note: print w/replies, xml ) Need Help??


in reply to Re: Newbie question
in thread Newbie question

I would strongly favor using the abs() function. One of the issues here is that if you have a statement like no warnings 'numeric';, then there should be some comments explaining why that is. abs() requires no explanation.

As far as speed with Perl, I am betting that abs() is really fast - this is not a complex operation for a 2's complement number. Don't know that Perl would be "smart" enough to do this asm code, but this is very fast because no branches to stall the instruction pipe.

; abs(eax), with no branches. ; intel syntax (dest, src) mov ebx, eax ; save copy of eax neg eax cmovl eax, ebx ; if eax is now negative, restore its saved value

Replies are listed 'Best First'.
Re^3: Newbie question
by NERDVANA (Deacon) on Aug 23, 2022 at 19:57 UTC
    Um, I hate to break it to you, but perl is fully interpreted with no just-in-time compilation. Every operation and every argument passed to it in a script is dozens of branches, no matter how innocent. The way to optimize a perl program is to code it with the fewest possible operations, fewest function calls, fewest assignments to a temporary variable, and even fewest curly-brace scopes. Optimizing a perl program is generally the opposite of making it more readable. (but there is a time and a place, etc)

    For a little mind-bender, try this:

    perl -MBenchmark -E ' my $x= 1; timethese(50000000, { mul => q{$x*$x}, sqrt => q{sqrt($x)} })'
Re^3: Newbie question
by oldB51 (Sexton) on Aug 23, 2022 at 12:58 UTC

    Thanks for your help.

Re^3: Newbie question
by Anonymous Monk on Aug 23, 2022 at 12:46 UTC
    Talk of optimization is premature?
      What I was trying to say is that the easiest to "code and understand" way is also likely to be the fastest way. And I was encouraging that. It really doesn't matter how good/bad the detailed implementation of abs() is, the time to execute the worst imaginable implementation is likely faster than making decisions about default behavior in light of an exception (the '-') and also suppressing normal exception behavior. I also suspect that either choice is completely dwarfed by the by the split operation and creation of the list of individual numbers.

      I don't think any benchmarking or further optimization is warranted here. I vote for the easy to understand way.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found