How would eq efficiently do this
While I'm always a little dubious of benchmarks but here's one for your given case
use Inline C; use POSIX qw( strncmp ); use Benchmark qw( cmpthese ); use strict; use warnings; my $n = 36; my @nums = 0 .. 99_999; my @str = map join('', 'a' .. 'z'), @nums; no warnings qw/ uninitialized void /; cmpthese(-10, { strncmp => sub { for(@nums) { strncmp_perl(@str[$_,$_ + 1], $n); } }, eq_substr => sub { for(@nums) { substr($str[$_], 0, $n) eq substr($str[$_ + 1], 0, $n) } }, eq_unpack => sub { for(@nums) { unpack("A$n", $str[$_]) eq unpack("A$n", $str[$_ + 1]) } }, }); __END__ __C__ int strncmp_perl(char* s1, char* s2, int n) { return strncmp(s1, s2, (int)n); } __output__ Benchmark: running eq_substr, eq_unpack, strncmp, each for at least 10 + CPU seconds... eq_substr: 11 wallclock secs (10.11 usr + 0.01 sys = 10.12 CPU) @ 5 +.34/s (n=54) eq_unpack: 11 wallclock secs (10.16 usr + 0.00 sys = 10.16 CPU) @ 2 +.07/s (n=21) strncmp: 11 wallclock secs (10.04 usr + 0.00 sys = 10.04 CPU) @ 5 +.88/s (n=59) Rate eq_unpack eq_substr strncmp eq_unpack 2.07/s -- -61% -65% eq_substr 5.34/s 158% -- -9% strncmp 5.88/s 184% 10% --
So it looks like the Inline::C strncmp() is slightly faster than substr() and eq combined, but always take benchmarks with a grain of salt.
HTH

_________
broquaint


In reply to Re: strncmp functionality by broquaint
in thread strncmp functionality by Anonymous Monk

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.