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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^7: An array of boolean values in a bitfield by BrowserUk
in thread An array of boolean values in a bitfield by kyle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.