Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Newbie question

by syphilis (Archbishop)
on Aug 23, 2022 at 11:08 UTC ( [id://11146307]=note: print w/replies, xml ) Need Help??


in reply to Newbie question

As an example, I have used the absolute value function abs but I could have used abs($_). Which is best? Does it matter?

It doesn't really matter which you use - either way, the same code gets executed.

Interestingly, there's no need to call on the abs() function at all.
You could replace:
my @digits = split //, abs; with: my @digits = split //, $_;
That will work fine because the "-" character evaluates to zero in numeric context, anyway.
With that change (and the other previously mentioned correction) in place, the script then outputs:
C:\_32\pscrpt>perl try.pl Argument "-" isn't numeric in addition (+) at try.pl line 10. Argument "-" isn't numeric in addition (+) at try.pl line 10. Argument "-" isn't numeric in addition (+) at try.pl line 10. -221 -21 1 1 3 5 21 34 89 144
The warnings can be silenced by inserting:
no warnings 'numeric';
into the oddDigitSum() subroutine:
sub oddDigitSum { no warnings 'numeric'; my @ans; for(@_) { my @digits = split //, $_; my $sum; $sum += $_ for @digits; $sum % 2 && push @ans, $_; } return @ans; }
Note that any "numeric" warnings triggered from outside the oddDigitSum() subroutine are still enabled.

I'm not sure which approach is the most efficient - you could use Benchmark; to find out, if you want.
I expect there's not much difference performance-wise.

Cheers,
Rob

Replies are listed 'Best First'.
Re^2: Newbie question
by Marshall (Canon) on Aug 23, 2022 at 11:44 UTC
    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
      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)} })'

      Thanks for your help.

      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.

Re^2: Newbie question
by oldB51 (Sexton) on Aug 23, 2022 at 12:49 UTC

    Fantastic re no warning numeric. Works fine without the abs.

      If you don't want to use abs and you don't want the warnings, there's always

      my @digits = /\d/aag;

      🦛

        Yes interesting. The regular expression avoids the use of split and abs. Regular expressions are someway down my learning list so I must simply note it for the future. Clearly Perl has many attack lines.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (2)
As of 2024-04-20 06:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found