in reply to Re: what's faster than .=
in thread what's faster than .=

Pre-allocating the string sounds like a good idea, but in order to make it work, you then need to ensure that you overwrite the pre-allocated contents, not add to or replace them.

That does not imply that you have to use substr or vec. Perl generally manages things for you as long as you use the same variable name. Part of the design of Perl was to not throw away information on the previous high-water mark when using a particular variable--even lexicals remember how much space they needed from the last time through, and assume you'll use that much again the next time through the loop or sub. Benchmarks on .= will produce much faster results the second time through, provided you don't recreate the actual variable.

Replies are listed 'Best First'.
Re: Re: Re: what's faster than .=
by BrowserUk (Patriarch) on Mar 10, 2003 at 18:09 UTC

    That's both interesting and intriguing. Is the behavior documented other than in the source? Is it reliable? How does Perl decide when to GC a subs lexicals and when not to? How does it know when a sub might be called again?


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      It's slightly documented in the Camel book on page 598: "Don't undef long strings and arrays if they'll be reused for the same purpose. This helps prevent reallocation when the string or arrayh must be re-extended."

      It's reliable till someone changes it... :-)

      Perl 5 never GCs a sub's lexicals in the sense we're talking about here. Certainly things can happen to objects that go out of scope, but that's different.

      Perl doesn't know when a sub might be called again. It merely assumes it.

        Perl 5 never GCs a sub's lexicals in the sense we're talking about here. Certainly things can happen to objects that go out of scope, but that's different.

        Curiouser and curiouser (as someone famous, literally speaking, once said:).

        Doesn't that imply that the often advised "scope as tightly as possible to reduce memory usage" is (at least some of the time) wrong? Or shouldn't I be asking such questions:)?


        Examine what is said, not who speaks.
        1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
        2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
        3) Any sufficiently advanced technology is indistinguishable from magic.
        Arthur C. Clarke.