Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^5: Abusing Map

by BrowserUk (Pope)
on May 17, 2018 at 16:34 UTC ( #1214758=note: print w/replies, xml ) Need Help??


in reply to Re^4: Abusing Map
in thread Abusing Map

I agree, and that's why I like, in particular, the for-loop and, less so, map (and let's just pass over the grep) versions that iterate over ranges of 1 .. $#array or 0 .. $#array-1 and so will never enter the loop

The problem with the map/grep and postfix for is that they generate a list as big as the array.

Small lists: meh! But for big lists -- where big frequently is in the 100s of millions on my machine and billions on client/AWS X1 instances -- those lists cost big time.

Postfix while and until avoid that whilst still giving clean, concise code. Combined with each, its good for big hashes also.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Replies are listed 'Best First'.
Re^6: Abusing Map
by AnomalousMonk (Bishop) on May 17, 2018 at 18:27 UTC
    ... for big lists ...

    I agree about the time/space advantages, sometimes life-saving advantages, of in-place mutation for big lists.

    ... postfix for ... generate[s] a list as big as the array.

    I thought a for-loop, postfix or otherwise, was guaranteed to have a space complexity (if that's the correct term) of zero | 1 (per ikegami here) — if it's written right! That, e.g., either
        for (@humongous_array) { $_ = $_ + $_ }
    or
        $_ = $_ + $_ for @humongous_array;
    are guaranteed to be just fine (if @humongous_array could fit in memory in the first place), whereas
        @humongous_array = map $_ + $_, @humongous_array;
    can kill you in terms of both time and space. In fact, isn't it the case that beginning with Perl version 5.mumble, map called in void context produces no intermediate list/array, so even
        map $_ = $_ + $_, @humongous_array;
    should be perfectly safe (although for aesthetic reasons, I'm not a fan of this particular usage)?

    And let's just not talk about Fight Club | grep anymore.


    Give a man a fish:  <%-{-{-{-<

      The problem comes when you need to build a list of indexes as with the OPs code:

      sub checkMem{ `tasklist /nh /fi "pid eq $_[0]"`; };; print checkMem( $$ );; perl.exe 3040 Console 1 +9,544 K $#a = 1e6;; print checkMem( $$ );; perl.exe 3040 Console 1 1 +7,428 K map{ $_ } 0 .. $#a;; print checkMem( $$ );; perl.exe 1224 Console 1 7 +3,272 K ### 56MB to build the list of 1e6 indices ###

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
        The problem comes when you need to build a list of indexes ...

        Ah, ok, I see now what you're talking about.


        Give a man a fish:  <%-{-{-{-<

        I remember reading that for ($start..$end) kind of loops are also optimized not to produce an intermediate list:

        $ perl -s -e' sub mymem { system("ps -q $$ -o vsz") }; mymem(); $#a = $size; mymem(); $a[$_]=0 for 0..$#a; mymem() ' -- -size=1e7 VSZ 18724 VSZ 96852 VSZ 333528 $ perl -s -e' sub mymem { system("ps -q $$ -o vsz") }; mymem(); $#a = $size; mymem(); $a[--$size]=0 while $size; mymem() ' -- -size=1e7 VSZ 18724 VSZ 96852 VSZ 333528 $ perl -E'say $^V' v5.24.1
        At least, they seem to be on my machine.

        This doesn't cover map, though:

        $ perl -s -e' sub mymem { system("ps -q $$ -o vsz") }; mymem(); $#a = $size; mymem(); map {$_} 0..$#a; mymem() ' -- -size=1e7 VSZ 18724 VSZ 96852 VSZ 573664

      I thought a for-loop, postfix or otherwise, was guaranteed to have a space complexity (it that's the correct term) of zero

      Of 1 (constant).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1214758]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2020-11-30 23:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?