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

Re: [FWP] Quick hack for common length



On Mon, Jun 25, 2001 at 04:20:09PM -0700, Clinton A. Pierce wrote:
> >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?)

This keeps the regex, while doing away with the list context and $1.

sub comlen {
   local($_) = $_[0] ^ $_[1];
   m/^\0*/g;
   pos;
}


Ronald

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