Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2018-06-22 00:34 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (120 votes). Check out past polls.