in reply to Re^3: how to read multiple line from a file
in thread how to read multiple line from a file

If a sliding window is needed, shift and push might be better.
This leads to extra memmove()s if you're lucky (I believe Perl does this), and endless memory consumption otherwise. If you use the mod operator when indexing your array, you get a ring buffer without excess copying. You could probably abstract this out nicely with a bit of extra work.
Just another Perler interested in Befunge Programming.
  • Comment on Re^4: how to read multiple line from a file

Replies are listed 'Best First'.
Re^5: how to read multiple line from a file
by LanX (Saint) on Apr 14, 2013 at 19:43 UTC
    > This leads to extra memmove()s if you're lucky (I believe Perl does this),

    Sure but I think we should try and measure before we optimize.

    > and endless memory consumption otherwise.

    Why? Someone .. (I think Grandfather) said once linked lists are rarely needed in Perl, because arrays are optimized in this way.(?!?)

    Anyway having an array were the newest line is somewhere in the middle results in arithmetical overhead.

    Slicing and copying this array in a sorted one leads to new overhead.

    I think try and measure is easier than digging into theoretical calculations...

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I think try and measure is easier than digging into theoretical calculations...
      This has nothing to do with theory. An array is a contiguous chunk of memory. If you put things on one side and take them from the other, you can either shift the current contents, or keep allocating more memory.
      Just another Perler interested in Befunge Programming.
        > This has nothing to do with theory.

        Nonsense! Sorry!

        Taking a short look into the Panther book convinces me that your understanding of AVs is rather oversimplified¹!

        There are mechanisms to optimize the effects of shifts and pushs!

        Anyway others are more qualified to talk about perlguts.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        ¹) this is not C !