Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Fast Identification Of String Difference

by educated_foo (Vicar)
on Jan 18, 2011 at 03:09 UTC ( #882802=note: print w/ replies, xml ) Need Help??

in reply to Fast Identification Of String Difference

I was curious, so...
use Inline C; sub cmp_perl { my ($a, $b) = @_; my $c = $a ^ $b; my @ret; push @ret, pos($c) while $c =~ /[^\0]/g; @ret; } use Benchmark ':all'; my ($x, $y); $y = $x = 'A' x 1e6; substr($y, $_*100, 1) = 'C' for 1..(1e4-1); timethese(0, { perl => sub { cmp_perl($x, $y); }, C => sub { cmp_c($x, $y); }, }); __END__ __C__ void cmp_c(char *a, char *b) { int i, n; Inline_Stack_Vars; Inline_Stack_Reset; for (i=0, n=0; *a && *b; a++, b++, i++) if (*a != *b) mXPUSHi(i), n++; Inline_Stack_Return(n); }
I was disappointed that the Perl version was about 8x slower:
Benchmark: running C, perl for at least 3 CPU seconds... C: 3 wallclock secs ( 3.17 usr + 0.00 sys = 3.17 CPU) @ 37 +9.18/s (n=1202) perl: 3 wallclock secs ( 3.17 usr + 0.00 sys = 3.17 CPU) @ 43 +.22/s (n=137)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://882802]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2016-07-24 09:52 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (221 votes). Check out past polls.