in reply to strncmp functionality

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

Replies are listed 'Best First'.
Re: Re: strncmp functionality
by Anonymous Monk on Apr 04, 2003 at 17:08 UTC
    Thanks for that. I used the following comparison in a benchmark:
    index(substr($str[$_], 0, $n), $str[$_ + 1], 0) != 0
    and came up with:
    Benchmark: running eq_index, eq_substr, each for at least 10 CPU secon +ds... eq_index: 14 wallclock secs (10.08 usr + 0.00 sys = 10.08 CPU) @ 3 +.08/s (n=31) eq_substr: 16 wallclock secs (10.30 usr + 0.00 sys = 10.30 CPU) @ 3 +.01/s (n=31) Rate eq_substr eq_index eq_substr 3.01/s -- -2% eq_index 3.08/s 2% --
    Thoughts or comments?

    -Lynn

    update (broquaint): added formatting