Now I haven't benchmarked this assertion,

Why such a hurry?

but for now I'll risk injury and insult by taking the authors' word that unpack is faster.

Hold on. Here it comes. :)

It looks like substr is faster than unpack ...

#!/usr/bin/perl -w use strict; use Benchmark qw(timethese); my $repeat = 10; my $text = ('abc' x $repeat) . 'gotcha' . ('xyz' x $repeat); my ($pre,$match,$post); print "OS: $^O - Perl: $]\n"; timethese( 100000, { 'unpack' => sub { if ($text =~ /gotcha/) { $pre = prematch($text); $post = postmatch($text); $match = match($text); } }, 'substr' => sub { if ($text =~ /gotcha/) { $pre = substr_prematch($text); $post = substr_postmatch($text); $match = substr_match($text); } }, } ); if ($text =~ /gotcha/) { print "unpack\n"; print "prematch :", prematch($text), "\n"; print "match :", match($text), "\n"; print "postmatch :", postmatch($text), "\n"; print "substring\n"; print "prematch :", substr_prematch($text), "\n"; print "match :", substr_match($text), "\n"; print "postmatch :", substr_postmatch($text), "\n"; } sub prematch { return unpack "a$-[0]", $_[0]; } sub postmatch { return unpack "x$+[0] a*", $_[0]; } sub match { my $len = $+[0] - $-[0]; unpack "x$-[0] a$len", $_[0]; } sub substr_match { substr( $_[0], $-[0], $+[0] - $-[0] ) } sub substr_prematch { substr( $_[0], 0, $-[0] ) } sub substr_postmatch { substr( $_[0], $+[0] ) } __END__ (output edited to fit the page better) OS: cygwin - Perl: 5.008 Benchmark: timing 100000 iterations of substr, unpack... substr: 2 wallclock secs ( 1.92 usr + 0.00 sys = 1.92 CPU) unpack: 11 wallclock secs (11.89 usr + 0.00 sys = 11.89 CPU) unpack prematch :abcabcabcabcabcabcabcabcabcabc match :gotcha postmatch :xyzxyzxyzxyzxyzxyzxyzxyzxyzxyz substring prematch :abcabcabcabcabcabcabcabcabcabc match :gotcha postmatch :xyzxyzxyzxyzxyzxyzxyzxyzxyzxyz OS: MSWin32 - Perl: 5.006001 Benchmark: timing 100000 iterations of substr, unpack... substr: 3 wallclock secs ( 1.93 usr + 0.00 sys = 1.93 CPU) unpack: 4 wallclock secs ( 3.87 usr + 0.00 sys = 3.87 CPU) unpack OS: linux - Perl: 5.006001 Benchmark: timing 100000 iterations of substr, unpack... substr: 2 wallclock secs ( 2.26 usr + 0.00 sys = 2.26 CPU) unpack: 4 wallclock secs ( 3.90 usr + 0.00 sys = 3.90 CPU) unpack

Update (1) And the reason is the overhead of interpolating $-[0] and $+[0] in the unpack parameter. If you run the benchmark with unpack "a30" and unpack "a36" it will be faster than substr but useless in general. So The Perl Cookbook was right after all. However, the devil is in the detail ... ;)

Update (2) The turning point is when the string is at least 5000 30,000 chars. With large strings, unpack becomes faster as advertised (Perl 5.6.1). Set $repeat to 5000, put an exit after timethese, and see the results. (Actually it is 5,000 groups of characters, thus creating a 30,000 chars string.)

Update (3) The original subs can be made slightly faster using sprintf instead of a simple interpolation. Using this version, the turning point, where the unpack version becomes faster than the substr implementation, is down to 3500 chars groups (= 21,000 chars).

sub prematch { unpack sprintf("a%d",$-[0]), $_[0]; } sub postmatch { unpack sprintf( "x%d a*", $+[0]) , $_[0]; } sub match { unpack sprintf ("x%d a%d", $-[0], $+[0] - $-[0] ), $_[0]; }
 _  _ _  _  
(_|| | |(_|><
 _|   

In reply to Re: RegExps, Prematch and Postmatch without efficiency penalty by gmax
in thread RegExps, Prematch and Postmatch without efficiency penalty by davido

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.