Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Just to test if it would bring speed (which I did not expect), I fiddled with pack and unpack. Before doing the hard part, I looked at the speeds halfway. It is not worth pursuing.

I also looked at Inline::C, and it is slower than your perl code (and I already optimized the stack stuff)

$ cat use 5.016; use warnings; use Inline "C"; use Benchmark qw( cmpthese ); my $n = 0x80061861; my $x = 10000; sub andshift { my ($top14, $nxt6, $mid6, $bot6); for (1 .. $x) { $top14 = ($n & 0xfffc0000) >> 18; $nxt6 = ($n & 0x0003f000) >> 12; $mid6 = ($n & 0x00000fc0) >> 6; $bot6 = ($n & 0x0000003f); } return ($top14, $nxt6, $mid6, $bot6); } # andshift sub packunpack { my ($top14, $nxt6, $mid6, $bot6); for (1 .. $x) { ($top14, $nxt6, $mid6, $bot6) = unpack "A14 A6 A6 A6", unpack "B32", => pack "N" => $n; } return ($top14, $nxt6, $mid6, $bot6); } # packunpack sub inlined { my ($top14, $nxt6, $mid6, $bot6); for (1 .. $x) { ($top14, $nxt6, $mid6, $bot6) = split14666 ($n); } return ($top14, $nxt6, $mid6, $bot6); } # inlined say for andshift (); say for packunpack (); say for split14666 ($n); cmpthese (-1, { andshift => \&andshift, bitstrings => \&packunpack, inline_c => \&inlined, }); __END__ __C__ void split14666 (int n) { Inline_Stack_Vars; Inline_Stack_Reset; EXTEND (sp, 4); mPUSHi (((unsigned int)n & 0xfffc0000) >> 18); mPUSHi (((unsigned int)n & 0x0003f000) >> 12); mPUSHi (((unsigned int)n & 0x00000fc0) >> 6); mPUSHi (((unsigned int)n & 0x0000003f) ); Inline_Stack_Done; } /* split14666 */ $ perl 8193 33 33 33 10000000000001 100001 100001 100001 8193 33 33 33 Rate bitstrings inline_c andshift bitstrings 121/s -- -52% -74% inline_c 250/s 108% -- -47% andshift 470/s 290% 88% --

Enjoy, Have FUN! H.Merijn

In reply to Re: Efficient bit-twiddling in Perl. by Tux
in thread Efficient bit-twiddling in Perl. by BrowserUk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (10)
    As of 2018-03-23 16:56 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (294 votes). Check out past polls.