Thank you, this is probably the best way of looking at it! I took your idea and expanded on it, below. It seems that the answer to my original question is No, there isn't any magic going on with for (split ...), it's just that while it may be a memory hog, split is still pretty fast - that's what threw me off initially.

#!/usr/bin/env perl use warnings; use 5.014; my $cnt = 5000000; system($^X,'-sE',<<'_END_','--',"-cnt=$cnt"); say `ps -orss $$`=~s/\s+/ /gr; # RSS 4340 $x = "abc\n" x $cnt; say `ps -orss $$`=~s/\s+/ /gr; # RSS 23936 @y = ("abc") x $cnt; say `ps -orss $$`=~s/\s+/ /gr; # RSS 455604 _END_ say "---"; system($^X,'-sE',<<'_END_','--',"-cnt=$cnt"); say `ps -orss $$`=~s/\s+/ /gr; # RSS 4544 $x = "abc\n" x $cnt; say `ps -orss $$`=~s/\s+/ /gr; # RSS 23972 @y = split /\n/, $x; say `ps -orss $$`=~s/\s+/ /gr; # RSS 423544 _END_ say "---"; system($^X,'-sE',<<'_END_','--',"-cnt=$cnt"); say `ps -orss $$`=~s/\s+/ /gr; # RSS 4540 $x = "abc\n" x $cnt; say `ps -orss $$`=~s/\s+/ /gr; # RSS 23964 for (split /\n/, $x) { say `ps -orss $$`=~s/\s+/ /gr; # RSS 457512 last } _END_ say "---"; system($^X,'-sE',<<'_END_','--',"-cnt=$cnt"); say `ps -orss $$`=~s/\s+/ /gr; # RSS 4336 $x = "abc\n" x $cnt; say `ps -orss $$`=~s/\s+/ /gr; # RSS 23932 open my $fh, '<', \$x or die $!; while (<$fh>) { say `ps -orss $$`=~s/\s+/ /gr; # RSS 24000 last } _END_

In reply to Re^2: Is foreach split Optimized? by haukex
in thread Is foreach split Optimized? (Update: No.) by haukex

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.