Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
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
Re^2: A mod2 Machine.
by choroba (Abbot) 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 about the Monastery: (14)
As of 2014-12-18 18:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (59 votes), past polls