Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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 test.pl 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 test.pl 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

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!
  • 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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (8)
    As of 2015-07-29 02:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (260 votes), past polls