I can't find the node at the moment but I remember BrowserUk posting a solution where taking references to substrs of a buffer gave better performance than using unpack. IIRC, this was because unpack had the overhead of re-parseing the template string every time it was invoked. This benchmark seems to bear that out.

use strict; use warnings; use Benchmark qw{ cmpthese }; use Test::More qw{ no_plan }; my $buffer = q{1130508154533}; my $revBuffer = q{3345150805113}; my $template = q{ a3 a2 a2 a2 a2 a2 }; my $rsYr = \ substr $buffer, 0, 3; my $rsMon = \ substr $buffer, 3, 2; my $rsDay = \ substr $buffer, 5, 2; my $rsHr = \ substr $buffer, 7, 2; my $rsMin = \ substr $buffer, 9, 2; my $rsSec = \ substr $buffer, 11, 2; open my $dataFH, q{<}, \ <<EOD or die $!; 1130508154533613 EOD my $start = tell $dataFH; my %methods = ( substr => sub { seek $dataFH, $start, 0; my $rev; while ( $buffer = <$dataFH> ) { $rev = join q{}, $$rsSec, $$rsMin, $$rsHr, $$rsDay, $$rsMon, $$rsYr; } return $rev; }, unpack => sub { seek $dataFH, $start, 0; my $rev; while ( $buffer = <$dataFH> ) { my @elems = unpack $template, $buffer; $rev = join q{}, reverse @elems } return $rev; }, ); foreach my $method ( sort keys %methods ) { ok( $methods{ $method }->() eq $revBuffer, $method ); } close $dataFH or die $!; open $dataFH, q{<}, \ <<EOD or die $!; 1130508154533613 1130508160033800 1130508161534113 1130508163034519 1130508164535019 1130508164535019 1130508170035191 1130508171533160 1130508173033660 1130508174534097 1130508180034535 1130424070116695 1130425070118790 1130426070118834 1130427070122888 1130428070123115 1130429070126161 1130430070127538 1130501070128195 1130502070131210 1130503070131969 1130504070135198 EOD $start = tell $dataFH; cmpthese( -10, { map { my $codeStr = q[sub { my $rev = $methods{ ] . $_ . q[ }->(); }]; $_ => eval $codeStr; } keys %methods } ); close $dataFH or die $!;

The output.

ok 1 - substr ok 2 - unpack Rate unpack substr unpack 17980/s -- -56% substr 40619/s 126% -- 1..2

I've cocked benchmarks up before so this might be wide of the mark but, hopefully, I have remembered BrowserUk's post correctly and this will be of use.

Update: This is BrowserUk's node that I was remembering.

Cheers,

JohnGG


In reply to Re: Candle Time by johngg
in thread Candle Time by Random_Walk

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.