Dear monks,

I have implemented the following code to read a binary stream and am currently getting outperformed by some Java code which is 10x faster.

turning on binmode on the file increased speed, but I am slowly running out of options to optimise this program.

There are small optimisations that can be done in the code such as moving to an if-else block rather than a dispatch table. However, this hasn't really made much of an effect on the speed.

Wise monks is there anything in this program that is causing this to not run at its best?

UPDATE: I took a look over my program and found the OUTPUT_AUTOFLUSH was set to 1, I removed this and it increased speed by about 30%

# main # my $t0 = Benchmark->new(); my $FEED; ($go{file}) ? open $FEED, "<:perlio", $go{file} : open $FEED, "<&", *STDIN; binmode $FEED; my %vtmf_type = ( 0 => \&read_int, 1 => \&read_byte, 2 => \&var_length, 3 => \&read_long, 4 => sub { return 0 }, 5 => \&escape, 6 => \&var_length, 7 => \&read_price, __DEFAULT__ => sub { $log->warn("skipping message"); return ('', 2 +55) } ); my $message_length_bin; while (read $FEED, $message_length_bin, 2) { my $message_length = unpack 'n', $message_length_bin; my $tag_bin; while (read $FEED, $tag_bin, 2) { my $tag_field = unpack 'B16', $tag_bin; my $type = oct('0b' . substr $tag_field, 0, 3); my $tag = substr $tag_field, 3, 16; my ($value, $tag_length) = ($vtmf_type{$type} || $vtmf_type{__ +DEFAULT__})->($FEED); print oct('0b' . $tag) . '=' . $value . ','; $message_length -= 2 + $tag_length; last unless $message_length; } print "\n"; } my $t1 = Benchmark->new(); if ($go{time}) { my $td = timediff($t1, $t0); $log->info("The code took: ", timestr($td)); } exit 0; # subroutines # sub read_int { my $handle = shift; read $FEED, my($num_bin), 4; my $value = hex unpack "H*", $num_bin; return ($value, 4); } sub read_long { my $handle = shift; read $handle, my($num_bin), 8; my ($hi, $lo) = map { hex $_ } (unpack 'A8A8', unpack 'H16', $num +_bin); my $num = $hi * 2**32 + $lo; return ($num, 8); } sub read_price { my $handle = shift; read $handle, my ($num_bin), 8; my ($hi, $lo) = map { hex $_ } (unpack 'A8A8', unpack 'H16', $num +_bin); my $num = $hi * 2**32 + $lo; $num /= 10**7; return ($num, 8); } sub read_byte { my $handle = shift; read $handle, my($num_bin), 1; my $val = unpack 'A', $num_bin; return ($val, 1); } sub escape { return ('', 0); } sub var_length { my $handle = shift; read $handle, my($length_bin), 1; my $length = hex unpack 'H2', $length_bin; read $handle, my($value_bin), $length; my $value = unpack "A${length}", $value_bin; return ($value, $length + 1); }

In reply to Reading binary file performance by oneill

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.