Perl is a pretty high level language. It’s optimized to handle cases like the one you’re referring to with decent efficiency, but makes little provision for low level techniques. foreach(LIST){$_} is faster than for($i=0;$i++;$i<@arr){$arr[$i]}; writing your own code to sort data is pretty much doomed to be slower than sort; goto is very slow, function calls aren’t nearly as much; the list goes on.

The fastest way to do something in Perl is frequently the one that implements the most costly step in the fewest ops. That’s why a Guttman-Rosler transform is faster than a straight Schwartzian transform for complex records, f.ex – because the costly operation is the sort, and the GRT does not burden that one with callback.

It is therefore no surprise that BrowserUk’s tests with substr failed to improve performance. Perl’s process of locating a specific byte in a scalar’s long string – where scalars are a multiply indirect structure to allow seamless growth –, creating an lvalue for it, and then assigning to that lvalue is far more involved than C’s simple process of doing some pointer math and dereferencing the result.

Perl is not C. The most efficient way of doing things in Perl is almost always to let the perl interpreter do as much of the job as possible, rather than spelling it out.

Makeshifts last the longest.


In reply to Re: what's faster than .= by Aristotle
in thread what's faster than .= by xafwodahs

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.