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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.