And to get the index with, and the two different cases:
#!/usr/local/bin/perl -w use Benchmark; $count = 1_000_000; my $thing = "thing"; print "Match eq => ", check_eq(), "\n"; print "Match rx => ", check_rx(), "\n"; print "Match idx => ", check_idx(), "\n"; timethese( $count, { 'eq' => sub{ check_eq() }, 'rx' => sub{ check_rx() }, 'idx' => sub{ check_idx() } } ); $thing = "asdjgfakjgfashdf___thing___asklfhklajsdhlajsdf"; print "Match eq => ", check_eq(), "\n"; print "Match rx => ", check_rx(), "\n"; print "Match idx => ", check_idx(), "\n"; timethese( $count, { 'eq' => sub{ check_eq() }, 'rx' => sub{ check_rx() }, 'idx' => sub{ check_idx() } } ); sub check_eq { ($thing eq "thing")?1:0; } sub check_rx { ($thing =~ /thing/)?1:0; } $i = 0; sub check_idx { ((index $thing,'thing') != -1)?1:0; }
Result:

Match eq => 1 Match rx => 1 Match idx => 1 Benchmark: timing 1000000 iterations of eq, idx, rx... eq: 4 wallclock secs ( 3.40 usr + 0.66 sys = 4.06 CPU) @ 24 +6305.42/s (n=1000000) idx: 6 wallclock secs ( 4.01 usr + 0.86 sys = 4.87 CPU) @ 20 +5338.81/s (n=1000000) rx: 5 wallclock secs ( 4.30 usr + 0.90 sys = 5.20 CPU) @ 19 +2307.69/s (n=1000000) Match eq => 0 Match rx => 1 Match idx => 1 Benchmark: timing 1000000 iterations of eq, idx, rx... eq: 3 wallclock secs ( 3.38 usr + 0.62 sys = 4.00 CPU) @ 25 +0000.00/s (n=1000000) idx: 6 wallclock secs ( 4.18 usr + 1.05 sys = 5.23 CPU) @ 19 +1204.59/s (n=1000000) rx: 6 wallclock secs ( 4.49 usr + 1.05 sys = 5.54 CPU) @ 18 +0505.42/s (n=1000000)


T I M T O W T D I

In reply to Re: Re: Which is better when doing a simple match? by Cine
in thread Which is better when doing a simple match? by mr.dunstan

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.