This claim doesn't sound right to me.

foreach my $gene ( @{ $first_clone->get_all_Genes() } ) { print $gene->stable_id(), "\n"; }
$first_clone->get_all_Genes() returns a reference to an array. This is de-referenced to make a list of all elements (this takes some memory), then the foreach iterates over that list of elements.

This is the same as:

my $gene_ref= $first_clone->get_all_Genes(); foreach my $gene (@$gene_ref) # or (@{$gene_ref}), the same. # extra {} only needed # when subscript is used { print $gene->stable_id(), "\n"; }
The second code:
my $genes = $first_clone->get_all_Genes(); while ( my $gene = shift @{$genes} ) { print $gene->stable_id(), "\n"; }
This code is going to create the list of @$genes just like the first code, but uses a different formulation of the iterator than "foreach". I would expect that this formulation uses the same amount of memory as the first code (i.e. that a complete de-referenced list of @$genes is built as a preliminary step). The claim appears to be that somehow this does not create a list of @$genes. I would expect that the memory usage of the second version is the same and probably runs a bit slower than the foreach() iterator would. It is certainly more obtuse from a coding style.

If this is not the case, then I would also like to hear about it. But on the surface, this claim appears to be incorrect.

Update:I didn't consider that some memory consuming operation would happen with: $gene->stable_id(). When I saw the print, I just was thinking that this printed some existing value. Kudos to BrowerUk.


In reply to Re: lazy loading and memory usage by Marshall
in thread lazy loading and memory usage by Anonymous Monk

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.