There are many factors to consider when comparing the performance of your two models.

First, substr() and unpack() behave differently:

Secondly, Perl treats scalar assignments different from list assignments. In fact, the scalar assignment op-code is implemented much simpler. Benchmark the difference between "($a) = (1)" and "$a = 1" to see the effect that this has. Although 15+ scalar assignments may be more expensive than a list assignment that involves 15+ values, the overhead of a list assignment dampens the overhead of having many scalar assignments.

The only true way of comparing the two approaches, is the method that you have chosen -- benchmarking.

If you need to improve the performance of your substr() model, one (ugly) approach may be to use one large list assignment ("@data = (substr(), substr(), ...)") the same as you do for unpack(). Again, only benchmarking can tell... :-)

UPDATE: substr() only uses magic if called in lvalue context.


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