Here's another benchmark.

use warnings; use strict; use Benchmark qw[ cmpthese ]; my @fields = ( { name => 'name1', start => 0, len=> 4 }, { name => 'name2', start => 3, len=> 7 }, { name => 'name3', start => 8, len=> 3 }, { name => 'name4', start => 0, len=> 10 }, { name => 'name5', start => 5, len=> 20 }, { name => 'name6', start => 11, len=> 14 }, { name => 'name7', start => 9, len=> 13 }, { name => 'name8', start => 2, len=> 2 }, { name => 'name9', start => 1, len=> 10 }, ); open FH, '<', $ARGV[ 0 ] or die $!; cmpthese -3, { 'unpack' => sub { my $unpack = ""; my $position = 0; for ( @fields ) { $unpack .= $_->{start} < $position ? 'X' . ( $position - $_- +>{start} ) : $_->{start} > $position ? 'x' . ( $_->{start} - $po +sition ) : ''; $unpack .= 'A' . $_->{len}; $position = $_->{start}+$_->{len}; } seek FH, 0, 0; while( my $text = <FH> ) { my @a = unpack $unpack, $text; } }, 'substr' => sub { seek FH, 0, 0; while( my $text = <FH> ) { my @a = map{ substr $text, $_->{start}, $_->{len} } @field +s; } } }; close FH; __END__ C:\test>for /l %i in (1,1,6) do holli data\alpha.1e%i C:\test>holli data\alpha.1e1 Rate unpack substr unpack 28603/s -- -80% substr 140302/s 391% -- C:\test>holli data\alpha.1e2 Rate substr unpack substr 400/s -- -26% unpack 540/s 35% -- C:\test>holli data\alpha.1e3 Rate substr unpack substr 40.3/s -- -27% unpack 54.9/s 36% -- C:\test>holli data\alpha.1e4 Rate substr unpack substr 4.06/s -- -28% unpack 5.61/s 38% -- C:\test>holli data\alpha.1e5 (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) s/iter substr unpack substr 2.47 -- -27% unpack 1.80 37% -- C:\test>holli data\alpha.1e6 (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) s/iter substr unpack substr 25.3 -- -29% unpack 18.0 40% --

If your files are bigger than a few lines, the unpack version starts winning quite quickly.

But by nowhere near as much as the other benchmarks would have you believe because once you factor in reading each line from the file, the IO time which is constant for both approaches, the parsing time becomes much less significant. Worth having, but much less than you thought.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: Challenge: Construct an unpack string by BrowserUk
in thread Challenge: Construct an unpack string by holli

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.