in reply to Re^4: An array of boolean values in a bitfield
in thread An array of boolean values in a bitfield

I'm still confused, ... UPDATE: after rereading the code my confusion vanished, (but I'm better leaving the monastry till this starting flue doesn't affect my concentration anymore ;)

Anyway an extra ord() should be faster than any internal hashfunction. I'm not sure about the usecase, but if you want to tie the functionality, the additional ord() wouldn't bother the user of your module.

  • Comment on Re^5: An array of boolean values in a bitfield

Replies are listed 'Best First'.
Re^6: An array of boolean values in a bitfield
by ysth (Canon) on Dec 04, 2008 at 08:20 UTC
    Anyway an extra ord() should be faster than any internal hashfunction.
    You can closely estimate the efficiency of perl code by counting the number of simple operations, and an ord() and array lookup is one more operation than a hash lookup. I would expect it to be about 25% slower. Feel free to benchmark, though.
      an ord() and array lookup is one more operation than a hash lookup.

      It depends upon how you break up the bitvector into its characters.

      If you use unpack'C*', then there is no need to use ord. And as unpack is faster than split, using an array can work out 3 times faster than a hash.

      And you can take that one step further. By using a lookup array twice the size and unpack'S*', you halve the number of lookups and get close to 8 times faster.

      #! perl -slw use strict; use Data::Dump qw[ pp ]; use Benchmark qw[ cmpthese ]; my %lookup = map{ chr( $_ ) => unpack '%8b*', chr; } 0 .. 255; sub lookupHash { my $n = 0; $n += $lookup{ $_ } for split'', $_[ 0 ]; return $n; } my @lookupC = map unpack( '%8b*', chr ), 0 .. 255; sub lookupArrayC { my $n = 0; $n += $lookupC[ $_ ] for unpack 'C*', $_[ 0 ]; return $n; } my @lookupS = map unpack( '%16b*', pack 'S', $_ ), 0 .. 65535; sub lookupArrayS { my $n = 0; $n += $lookupS[ $_ ] for unpack 'S*', $_[ 0 ]; return $n; } our $bitvector = pack 'C*', ( 0 .. 255 ) x 10; cmpthese -1, { hash => q[ our $bitvector; my $n = lookupHash( $bitvector ); +], arrayC => q[ our $bitvector; my $n = lookupArrayC( $bitvector ); +], arrayS => q[ our $bitvector; my $n = lookupArrayS( $bitvector ); +], }; __END__ C:\test>junk Rate hash arrayC arrayS hash 197/s -- -79% -89% arrayC 930/s 373% -- -46% arrayS 1717/s 773% 85% --

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      Arrays with ord() are 5-10% faster ... at least on my (8 years) old machine.

      IMHO doesn't worth it, but feel free to benchmark though! ;)

      Cheers Rolf

      UPDATE: I wonder if the inline cache of newer processors might boost the opcode execution.