[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Quick hack for common length



>From fwp-l Mon Jun 25 15:26:34 2001

In #perl today, we were looking at a longest substrings problem,
utilizing a suffix tree solution.  Part of the C code was:

int comlen(char *p, char *q)
{
	i=0;
	while *p && (*p++ == *q++)
		i++;
	return i;
}

A straight translation from C to Perl yeilded something slow.

The quickest solution I could come up with in perl looked like:

sub comlen_or {
	length((($_[0]^$_[1])=~m/^(\0+)/)[0]);
}

Can anyone improve on that for speed?  I was actually happy with
(and then disappointed) with the regular expression.  Is there a
clever way to get rid of it, that list context and $1?
(unpack and bretheren maybe?)

Enjoy.

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe