To amplify what sgifford said: it would appear that the longer the string being matched, the bigger the difference between the two methods. For example, using sgifford's script, but with a $s of one character, the numbers on my machine are:

Rate brackets slash i brackets 719424/s -- -7% slash i 775194/s 8% --

While, when $s is 16KB long, the difference is more pronounced, with the following:

Rate brackets slash i brackets 3508/s -- -66% slash i 10434/s 197% --

Now the interesting question: sgifford's regex is very simple. What happens when a more complicated test is used. When using the following test code:

#!/usr/bin/perl -w use strict; use Benchmark 'cmpthese'; our $s = "HELLOWORLD" x 1024; cmpthese(1_000_000, { 'slash_i' => sub { $s =~ m/hello/i; $s =~ m/jello +/i }, 'brackets' => sub { $s =~ m/[Hh][Ee][Ll][Ll][Oo]/ +; $s =~ m/[Jj][Ee][Ll][Ll][Oo]/ +; }, });

produce the following results:

Rate brackets slash_i brackets 5633/s -- -66% slash_i 16683/s 196% --

It would appear that the length of the string amplifies the difference more than the complexity of the regular expression. (At least when comparing /[Tt]/ with /t/i.)


In reply to Re: Did the inefficiency of /i get fixed? by Belgarion
in thread Did the inefficiency of /i get fixed? by Cody Pendant

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.