Just looking at the faster versions and removing everything that is not the function (ie one true, one false test makes a set)...

#!/usr/bin/perl use Inline C =>; use Benchmark 'cmpthese'; # Rate tchn tchn2 kval LR tye2 tye1 # tchn 556060/s -- -9% -19% -20% -50% -64% # tchn2 611783/s 10% -- -11% -12% -45% -60% # kval 690724/s 24% 13% -- -0% -38% -55% # LR 691904/s 24% 13% 0% -- -38% -55% # tye2 1114446/s 100% 82% 61% 61% -- -28% # tye1 1542899/s 177% 152% 123% 123% 38% -- my $str_a = 'a'x255; my $str_b = 'b'x255; my $str_c = $str_a; my ( $fc ); cmpthese -5, { 'tchn' => sub { $a = same_scan( $str_a, $str_b ); $a = same_scan( $str_a, $str_c ); }, 'tchn2' => sub { $a = same_scan2( $str_a, $str_b ); $a = same_scan2( $str_a, $str_c ); }, 'LR' => sub { $a = index($str_a, substr($str_b, 0, 1)); $a = index($str_a, substr($str_b, 0, 1)); }, 'tye1' => sub { $a = ord $str_a != ord $str_b; $a = ord $str_a != ord $str_c; }, 'tye2' => sub { $fc = ord $str_a; $a = $fc != ord( $str_b ); $fc = ord $str_a; $a = $fc != ord( $str_b ); }, 'kval' => sub { $fc = substr $str_a, 0, 1; $a = $fc ne substr $str_b, 0, 1; $fc = substr $str_a, 0, 1; $a = $fc ne substr $str_c, 0, 1; }, }; __END__ __C__ int same_scan(char* str1, char* str2) { return str1[0] == str2[0] ? 1 : 0; } int same_scan2(SV* str1, SV* str2) { return SvPV(str1, PL_na)[0] == SvPV(str2,PL_na)[0] ? 1 : 0; }

cheers

tachyon


In reply to Re: Re: Matching First Character of Strings Efficiently by tachyon
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.