Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Trouble getting size of list returned from sub

by ColonelPanic (Friar)
on Nov 26, 2012 at 10:34 UTC ( #1005608=note: print w/replies, xml ) Need Help??

in reply to Trouble getting size of list returned from sub

Here is a solution that potentially uses less memory:
my $size = scalar map {1} big_list;

Explanation: All of the other options involve copying the return arguments into an array behind the scenes. This does not, instead creating an array with the same number of elements, but containing the value "1" for each element. This will use less memory if the the sub is returning a list of large items (such as long strings).

This would be worth trying if you have problems with high memory usage.

(To test this, see the sample code with memory usage test above)

When's the last time you used duct tape on a duct? --Larry Wall

Replies are listed 'Best First'.
Re^2: Trouble getting size of list returned from sub
by Anonymous Monk on Nov 26, 2012 at 11:17 UTC

    Actually they all do that, they copy the list to the "stack", its just that map introduces a scope, by the time it ends, perl has a chance to release the memory it used back to the OS -- an optimization

    For a long time map/grep in scalar/void context took double the memory than equivalent for loop, so depending on perl version memory usage will differ between these two lines

    warn scalar grep {1} foo(); my $count = 0; $count++ for foo(); warn $count ;

    wantarray is what you want to use if you want to be sure :)

      Actually, it is not a scoping issue. By that logic, this should also save memory:
      sub get_size { my @a = big_list(); return scalar @a } my $size = get_size();

      However, in a simple test, that uses the same memory as a global array that does the same thing.

      Perl, generally speaking, does not release memory back to the OS unless your system is running out of memory. The memory from lexical variables can be claimed and reused by Perl, but it doesn't go back to the OS.

      All of the other methods created an array in addition to what was copied on the stack. map only creates an array of 1s in addition to what was copied on the stack; thus it uses less memory (unless your original data is no bigger than integers, of course) (and yes, this was not always true in older versions of Perl).

      When's the last time you used duct tape on a duct? --Larry Wall

        Actually, it is not a scoping issue

        Sure it is

        By that logic, this should also save memory:

        No, you're copying the list, same as map used to do, even in void context, instead of merely aliasing it -- @_ is alias until you copy it, but you figured that out yourself in Re: Trouble getting size of list returned from sub

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005608]
[Corion]: I guess I should not read some discussions on HackerNews - the crowd there is weirdly different - "you have to stay current in tech" (by following all the latest JS trends) is a meme there, but it doesn't really match my experience
[Corion]: At least in my area, experience can beat most new tech anyway because most new tech is just a rehash of things abandoned in the 70s and we still have a lot of that ;)
[Corion]: choroba: Ah, that will be next Friday for $work ;) Drinking enough water is key ;))
[Corion]: But then, maybe that's just my experience with things, and maybe Hacker News is just people much younger who haven't seen more than one tech cycle...
[choroba]: s/water/bear/ and s/enough/too much/
[choroba]: beer
[choroba]: oh ok
[marto]: I crashed the ScotLUG Christmas night, having never actually been to ScotLUG
[choroba]: Corion yeah, I probably already told you about how Bjarne Stroustrup was asked whether he still watched the new tech trends and what really impressed him
[choroba]: His reply was "I watch them, but I haven't been impressed in the last 10 years. There's been nothing new". That was 2 years ago :)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2016-12-09 10:19 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (150 votes). Check out past polls.