in reply to Memory /speed questions

Perl does trade off memory for speed in quite a few places so if you are looking for super memory efficiency, Perl is probably not the best choice. That being said:

You are always (or almost always) going to trade off memory for speed. You just have to decide which is more important.

Update: Benchmarked the part I hadn't benchmarked yet.

Update2: Added more memory wasters.

Replies are listed 'Best First'.
Re: Re: Memory /speed questions
by BrowserUk (Patriarch) on Jul 30, 2003 at 05:43 UTC

    Using large lists in a foreach, ie. foreach (1..10000) { }.

    Whilst this used to be so, in recent versions of perl, (5.6.x/5.8.x) it is no longer so.

    Neither for (1 .. 10_000_000) { ... }

    nor  for ( @a ) { ... }

    will consume an (extra) memory as they are implemented as iterators. In the latter case, the control variable or $_ is aliased to the array element, so even if @a is huge, no additional memory is consumed by the construct.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: Re: Memory /speed questions
by liz (Monsignor) on Jul 30, 2003 at 07:45 UTC
    ...large data structures all stored in memory instead of on disk with a tied hash or array..

    In (prefork) mod_perl it's quite a common technique to load lots of (constant) stuff into memory at server startup, as these will get shared between all the children. And save you CPU and disk accesses on each request So it depends on your situation whether this really is a "waster" or not.

    Liz