### 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 (Bishop) 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. :)

Create A New User
Node Status?
node history
Node Type: note [id://1043442]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2018-06-24 23:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Should cpanminus be part of the standard Perl release?

Results (126 votes). Check out past polls.

Notices?