Actually, I had previously tried it using assignment rather than push and the difference was so negligable as to be within the bounds of varience from one run to the next.

C:\test>226666 Benchmark: running substr_it, unpack_it , each for at least 3 CPU seconds ... substr_it: 4 wallclock secs ( 3.17 usr + 0.00 sys = 3.17 CPU) @ 23 +75.99/s (n=7520) unpack_it: 4 wallclock secs ( 3.38 usr + 0.00 sys = 3.38 CPU) @ 43 +71.56/s (n=14754) Rate substr_it unpack_it substr_it 2376/s -- -46% unpack_it 4372/s 84% -- Results tally C:\test>

I did verify the original as best I could, but as he didn't supply the whole benchmark I had to make a few guesses.

I also attempted to improve both as far as I could as you will see from the update to my first post based upon your suggestion :^)

Older benchmark code for the above results

#! perl -slw use strict; use vars qw[$line @s @s2 @u1 @u2]; use Benchmark qw[cmpthese]; $line = (join'','a' .. 'y') x 40; sub substr_it{ my @bits; $bits[0] = substr $_[0], 413, 30; $bits[1] = substr $_[0], 373, 30; $bits[2] = substr $_[0], 343, 30; $bits[3] = substr $_[0], 245, 15; $bits[4] = substr $_[0], 679, 30; $bits[5] = substr $_[0], 10, 10; $bits[6] = substr $_[0], 900, 100; $bits[7] = substr $_[0], 500, 2; $bits[8] = substr $_[0], 313, 30; $bits[9] = substr $_[0], 844, 44; $bits[10] = substr $_[0], 111, 1; $bits[11] = substr $_[0], 100, 100; $bits[12] = substr $_[0], 454, 30; $bits[13] = substr $_[0], 240, 3; $bits[14] = substr $_[0], 236, 4; @bits; } sub unpack_it{ my $fmt = '@413A30 @373A30 @343A30 @245A15 @679A30 @10A10 @900A100 +' . '@500A2 @313A30 @844A44 @111A1 @100A100 @454A30 @240A3 @ +236A4'; unpack( $fmt, $_[0]); } cmpthese( -3, { substr_it => '@s = substr_it $line;', unpack_it => '@u1 = unpack_it $line;', }); print 'Results tally'; __END__

Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.


In reply to Re: Re: Re: Unexpected: unpack slower than several substr's. Why? by BrowserUk
in thread 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.