Do you know where your variables are? PerlMonks

### Re: eq vs. ==

by Abigail-II (Bishop)
 on Jul 21, 2003 at 13:45 UTC ( #276274=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Re: eq vs. ==

Was your point trying to prove chromatics point? Because IMO, all your benchmark shows is that having to do an atoi takes time. Because in both tests, the left hand side of the operator is the outcome of a module operation, the %. And that's numeric.

Here's a different benchmark. One that used both numbers and string, and both == and eq. You will see that the fastest cases are when no conversions needs to take place. Conclusion: use eq when comparing strings, and == when comparing numbers if speed is your main motivation.

```#!/usr/bin/perl

use strict;
use warnings;
use Benchmark qw /cmpthese/;

our (\$a, \$b, \$c, \$d);

cmpthese -10 => {
'==-num' => '\$::a = 0;
for my \$i (0 .. 1000000) {
\$::a ++ if \$i == 100;
}',
'eq-num' => '\$::b = 0;
for my \$i (0 .. 1000000) {
\$::b ++ if \$i eq "100";
}',
'==-str' => '\$::c = 0;
for my \$i ("0" .. "1000000") {
\$::c ++ if \$i == 100;
}',
'eq-str' => '\$::d = 0;
for my \$i (0 .. "1000000") {
\$::d ++ if \$i eq "100";
}',
};

print "[\$a] [\$b] [\$c] [\$d]\n";

__END__
Benchmark: running ==-num, ==-str, eq-num, eq-str, each for at least 1
+0 CPU seconds...
==-num: 10 wallclock secs (10.02 usr +  0.00 sys = 10.02 CPU) @  4
+.19/s (n=42)
==-str: 10 wallclock secs (10.25 usr +  0.01 sys = 10.26 CPU) @  1
+.75/s (n=18)
eq-num: 10 wallclock secs (10.06 usr +  0.01 sys = 10.07 CPU) @  2
+.38/s (n=24)
eq-str: 10 wallclock secs (10.28 usr +  0.00 sys = 10.28 CPU) @  2
+.43/s (n=25)
Rate ==-str eq-num eq-str ==-num
==-str 1.75/s     --   -26%   -28%   -58%
eq-num 2.38/s    36%     --    -2%   -43%
eq-str 2.43/s    39%     2%     --   -42%
==-num 4.19/s   139%    76%    72%     --
[1] [1] [1] [1]

Abigail

Create A New User
Node Status?
node history
Node Type: note [id://276274]
help
Chatterbox?
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?