I'm trying to squeeze some more speed out of some code that I've got.

The code in question carries out 15 substr operations for each line of input.
I was expecting a single unpack to be faster, however this wasn't the case.

Could anyone explain what's going on?

Which modules could help trace this sort of issue? Things like Devel::DProf aren't quite the right tool.

Cheers.

BazB.


Benchmarks and code follow...

This is perl, v5.8.0 built for i386-linux-thread-multi
Benchmark: timing 100000 iterations of substr, unpack... Rate unpack substr unpack 40486/s -- -28% substr 55866/s 38% --
The code containing the pack/substr is generated using positions slurped from a data dictionary, then eval'ed producing the following:
sub { my $line = shift; my @data; $data[0] = substr($line, 413, 30); $data[1] = substr($line, 373, 30); $data[2] = substr($line, 343, 30); # etc... $data[12] = substr($line, 454, 30); $data[13] = substr($line, 240, 3); $data[14] = substr($line, 236, 4); return @data; }; #------------------------------------- sub { my $line = shift; my @data = unpack('@413A30 @373A30 @343A30 # etc... @454A30 @240A3 @236A4', $line); };

In reply to Unexpected: unpack slower than several substr's. Why? by BazB

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.