With today's computers, I do not see any reason to worry about the few bytes or even a few kB that you are going to save with one solution to create your subs compared to another. I think your main aim should be to make your code clear to read and to understand and easy to maintain, don't worry about a few kilobytes memory footprint.

If it came to processing a huge file (say 10 GB, or perhaps even 1 GB)), then it would really make sense to think before choosing the algorithm whether it will store the whole file into memory or whether it will gently iterate over lines or chunks of the file. That's a totally different context, however.

But choosing between between various ways of implementing your subs (named subs, code_refs or anonymous subs, closures, class methods, etc., don't worry about memory usage, this is really irrelevant in most cases.

One slight warning, though: watch out for possible memory leaks and similar problems in your implementation (circular references, perhaps deep recursion, etc.), especially if your program is going to do really a lot of work or to run for a fairly long time. And even then, a memory link is not necessarily dramatic if you can ascertain that it will be only a few kilobytes and are sure that the program will always complete quickly enough so that the leak will never become a problem.


In reply to Re: Memory efficiency, anonymous vs named's vs local subroutines by Laurent_R
in thread Memory efficiency, anonymous vs named's vs local subroutines by thanos1983

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.