[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:
> 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?


use Inline C => <<EOF;
int comlen(char *p, char *q) {
        int i = 0;
        while( *p && (*p++ == *q++) )  
                i++;
        return i;
}
EOF

C is fast for these things, why fight it? :)


-- 

Michael G. Schwern   <schwern@pobox.com>    http://www.pobox.com/~schwern/
Perl6 Quality Assurance     <perl-qa@perl.org>       Kwalitee Is Job One
I've just gone through a lung-crushing breakup with my blender and I don't 
think I should screw my forehead alone tonight.

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