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

Re: [Fun With Perl] Another oddity from the days of assembler



From: Todd Larason <jtl-perl-funwith@molehill.org>
>On 990614, Roland Giersig wrote:
> >  sub s {
> >    my $b = unpack("B*", pack("L*", shift));
> >    $b =~ s/0+//g;
> >    return length($b);
> >  }
>
>sub s {
>   return eval join '+', split '',unpack("B*",pack("L*",shift));
>}
>
>Probably less efficient, but it takes advantage of perl's dynamic 
>expression evaluation capability.

That's wierd :)

But ok, if we're going to be wierd, why not do

sub s {
   return eval join '+',
               grep $_,
               split '',
               unpack("B*", pack("L*",shift));
}

I'm guessing, but it's probably more efficient to not add the 0's...

Ok, a quick run through Benchmark confirms it; adding the grep makes this 
the second slowest way to count 1 bits :)  The slowest is the eval without 
the grep.
--
Mike, who's having way too much fun reading this list...


_______________________________________________________________
Get Free Email and Do More On The Web. Visit http://www.msn.com

==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org

</x-flowed>