Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

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

Dear Monks,

I'm trying to find the best way to code/decode binary data to be transferred between client machines and server machine(s). Different operating systems change or delete characters in the stream. Pack/unpack is better than any regex I could come up with, but it doubles the size of the data transferred. MIME::Base64 is better than pack/unpack. The 'Simple Copy' step is obviously the fastest possible but can't be used for inter-machine communication.

Is there something better than MIME::Base64?

This script shows the best of what I have tested:

use strict; use Benchmark qw(:all); use MIME::Base64; our $src = ""; for ( 0..255 ) { $src .= chr( $_ ); } my $count = 0; print "case1: pack/unpack -- ", &case1, " size, $count, \n"; print "case2: MIME::Base64 -- ", &case2, " size, $count, \n"; print "case3: Simple Copy -- ", &case3, " size, $count, \n"; timethese ($count, { case1 => sub {&case1}, case2 => sub {&case2}, case3 => sub {&case3}, }, ); cmpthese ($count, { case1 => sub {&case1}, case2 => sub {&case2}, case3 => sub {&case3}, }, ); sub case1 { # print "case1\n"; our $src; my $result = unpack("H*", $src ); my $back = pack("H*", $result ); if ( $src ne $back ) { print "1 ne\n"; exit; } return length( $result ); } sub case2 { # print "case2\n"; our $src; my $result = encode_base64($src,""); my $back = decode_base64( $result ); if ( $src ne $back ) { print "2 ne\n"; exit; } return length( $result ); } sub case3 { # print "case3\n"; our $src; # my $result = $src; my $back = $src; if ( $src ne $back ) { print "3 ne\n"; exit; } return length( $back ); } __END__ email7:/usr/local/apache/prefork/cgi-bin/t# pyrperl Hex_vs_B64.plx case1: pack/unpack -- 512 size, 0, case2: MIME::Base64 -- 344 size, 0, case3: Simple Copy -- 256 size, 0, Benchmark: running case1, case2, case3 for at least 3 CPU seconds... case1: 4 wallclock secs ( 3.14 usr + 0.00 sys = 3.14 CPU) @ 14 +2625.48/s (n=447844) case2: 3 wallclock secs ( 3.11 usr + 0.00 sys = 3.11 CPU) @ 20 +4192.93/s (n=635040) case3: 4 wallclock secs ( 3.18 usr + 0.00 sys = 3.18 CPU) @ 11 +99358.18/s (n=3813959) Rate case1 case2 case3 case1 141559/s -- -31% -88% case2 205511/s 45% -- -83% case3 1203509/s 750% 486% --

Thank you

"Well done is better than well said." - Benjamin Franklin


In reply to Best technique to code/decode binary data for inter-machine communication? by flexvault

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 romping around the Monastery: (13)
    As of 2015-07-01 16:25 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 (11 votes), past polls