Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Candle Time

by johngg (Abbot)
on May 16, 2013 at 21:24 UTC ( #1033908=note: print w/ replies, xml ) Need Help??


in reply to Candle Time

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


Comment on Re: Candle Time
Select or Download Code
Re^2: Candle Time
by Random_Walk (Parson) on May 16, 2013 at 21:54 UTC

    johngg++ and of course BrowserUK

    This is just the kind of cunning solution that I was hoping for

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1033908]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-11-24 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (137 votes), past polls