Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

Replies are listed 'Best First'.
Re^2: A mod2 Machine.
by choroba (Chancellor) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043442]
[stevieb]: I have fixed a boatload of issues across several of my distributions, including a couple that have been nagging for a very long time and were exceptionally difficult to solve. Saving $ before we move, so thought I'd have a code session...
[stevieb]: ...session weekend. Went far better than I could have imagined. Even got a bunch of stuff soldered for another one of my electronics projects on the go, and am working on a fix for a MetaCPAN::Client issue I found
[shmem]: the smaller your boat is, the smaller is the load...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2017-06-25 22:24 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (571 votes). Check out past polls.