Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: A mod2 Machine.

by zork42 (Monk)
on Jul 10, 2013 at 10:37 UTC ( #1043442=note: print w/ replies, xml ) Need Help??


in reply to A mod2 Machine.

I wonder how the speed of

my $n = 534587; print "even\n" if($n % 2 == 0); # modulo operator of possibly a flo +at or possibly an integer (*)
compares with
use integer; my $n = 534587; print "even\n" if($n & 1 == 0); # checking LSB with Bitwise And on +an integer
(*) perlop: Integer-Arithmetic says
"By default, Perl assumes that it must do most of its arithmetic in floating point."

=====

Also I wonder how this
my $n = "534587"; print "even\n" if($n =~ /[02468]$/); # reg exp
compares with:
my $n = "534587"; my $c = substr($n, -1, 1); # last char print "even\n" if ( ($c == '0') || ($c == '2') || ($c == '4') || ($c = += '6') || ($c == '8') ); # no regexp


Comment on Re: A mod2 Machine.
Select or Download Code
Replies are listed 'Best First'.
Re^2: A mod2 Machine.
by choroba (Canon) on Jul 10, 2013 at 11:35 UTC
    Use Benchmark to find out.

    Also, using floats to compute modulo does not seem reasonable to me.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      I was just about to say that! zork42 please do benchmark it and post the results. PS: I'm really very lazy to do that and plus I'm busy perusing the perldoc's PerlRE tutorial. :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2015-07-08 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (98 votes), past polls