Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

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

Here's my attempt. There's two versions of the functions here. The first uses multiplication and division to implement something along the lines of what you say, where each possible six character string is mapped to a the integers 0 .. 2_565_726_408, encoded as 4 bytes.

The second deals with the first three bytes and the second three bytes separately, mapping each to an integer in the range 0 .. 50652, and encoding each to strings of length 2 bytes, 4 bytes altogether.

Although I haven't benchmarked them, my gut tells me that the second is faster. Whatsmore, the second can run within a "use integer" block, which allows Perl to use fast integer maths. The first will not run within "use integer" because it sometimes overflows.

#!/usr/bin/perl use strict; use warnings; { use bytes; my %lookup; my @reverse; my $scale; BEGIN { my $x = 0; my @chars = (' ', 0..9, 'A'..'Z'); keys %lookup = 65_536; # preallocate hash buckets for my $i (@chars) { for my $j (@chars) { for my $k (@chars) { $lookup{ $i.$j.$k } = $x; $reverse[$x++] = $i.$j.$k; } } } $scale = $x; } # Functions using multiplication... sub alphanum_to_bytes_M { my $head = $lookup{ substr($_[0], 0, 3) }; my $tail = $lookup{ substr($_[0], 3, 3) }; my $n = ($head * $scale) + $tail; pack(N => $n) } sub bytes_to_alphanum_M { my $n = unpack(N => $_[0]); my $head = int($n / $scale); my $tail = $n - ($head * $scale); $reverse[$head] . $reverse[$tail] } # Functions using bitshifting... { use integer; sub alphanum_to_bytes_B { my $head = $lookup{ substr($_[0], 0, 3) }; my $tail = $lookup{ substr($_[0], 3, 3) }; pack(nn => $head, $tail) } sub bytes_to_alphanum_B { join q{}, @reverse[ unpack(nn => $_[0]) ] } } # Function to pretty-print byte strings for display purposes... sub show_bytes { my ($str) = @_; sprintf 'bytes[%s]', join q{ }, map { sprintf('%02x', ord(substr($str, $_, 1))) } 0 .. length($str)-1 } } my @lines = <DATA>; print "MULTIPLICATION:\n"; foreach (@lines) { my $b; chomp; printf( "'%s' => '%s' => '%s'\n", $_, show_bytes($b = alphanum_to_bytes_M($_)), bytes_to_alphanum_M($b), ); } print "BIT SHIFTING:\n"; foreach (@lines) { my $b; chomp; printf( "'%s' => '%s' => '%s'\n", $_, show_bytes($b = alphanum_to_bytes_B($_)), bytes_to_alphanum_B($b), ); } __DATA__ 0 1 A Z ABCDEF ABCDEG ZAAAAA ZZZZZZ

In reply to Re: Fast - Compact That String by tobyink
in thread Fast - Compact That String by Limbic~Region

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, 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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (9)
    As of 2018-06-22 00:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?



      Results (120 votes). Check out past polls.

      Notices?