in reply to Memory efficiency, anonymous vs named's vs local subroutines

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.

  • Comment on Re: Memory efficiency, anonymous vs named's vs local subroutines

Replies are listed 'Best First'.
Re^2: Memory efficiency, anonymous vs named's vs local subroutines
by thanos1983 (Parson) on Jul 18, 2015 at 19:33 UTC

    Hello Laurent_R,

    Thank you for your time and effort, reading and replying to my question. You are right, maintenance comes top in the list. A few Bytes or even KBytes with the resources that we have today will not make any difference. Memory leakage is something that I should be aware and careful for.

    My way of moving forward would be using named functions is to understand and to implement. Small pieces of code to construct something bigger.

    Seeking for Perl wisdom...on the process of learning...not there...yet!