Including 'expensive_function' in a benchmark means that you are mostly measuring how often that function gets called (which isn't what you asked for) and reduces the differences in the remaining items to the level of noise (which a worse benchmark).

I commented out the guts of expensive_function, shortened the names, added tye2 which does what I thought was the obvious optimization of pulling out the first char of $str_a at the top:

'tye2' => sub { my $f = ord($str_a); for my $str_b ( @list ) { next if $f != ord( $str_b ); expensive_function ( $str_a, $str_b ); } },
and got these results:
Rate hb1 hb2 L~R3 BUk L~R2 dlrm L~R tye1 kvale ty +e2 hb1 46.5/s -- -35% -35% -38% -60% -62% -70% -73% -73% -7 +4% hb2 71.4/s 53% -- -0% -6% -38% -42% -54% -59% -59% -6 +1% L~R3 71.4/s 53% 0% -- -6% -38% -42% -54% -59% -59% -6 +1% BUk 75.7/s 63% 6% 6% -- -35% -39% -51% -56% -56% -5 +8% L~R2 116/s 149% 62% 62% 53% -- -6% -25% -33% -33% -3 +6% dlrm 124/s 166% 73% 73% 63% 7% -- -20% -28% -29% -3 +2% L~R 155/s 233% 117% 117% 105% 34% 25% -- -10% -11% -1 +4% tye1 172/s 270% 141% 141% 128% 48% 39% 11% -- -1% - +5% kvale 174/s 274% 143% 143% 130% 50% 41% 12% 1% -- - +4% tye2 181/s 289% 154% 154% 139% 56% 46% 17% 5% 4% +--

But I still consider such nano-optimization games to be more a waste than of value. (:

- tye        


In reply to Re^2: Matching First Character of Strings Efficiently (benchmarking) by tye
in thread Matching First Character of Strings Efficiently by Limbic~Region

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.