Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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


In reply to Re: eq vs. == by Abigail-II
in thread eq vs. == by c

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (4)
    As of 2014-07-26 19:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (178 votes), past polls