Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: how apply large memory with perl?

by BrowserUk (Pope)
on Aug 09, 2012 at 11:02 UTC ( #986487=note: print w/ replies, xml ) Need Help??


in reply to Re^2: how apply large memory with perl?
in thread how apply large memory with perl?

I don't know internal details of .. op and $#, but what makes so big difference?

When you use the range operator outside of a for loop, Perl creates that list on one of its internal stacks first. This requires ~650MB, and is best illustrated by creating that mythically non-existant "list in a scalar context":

C:\test>perl -E"scalar( ()=1..9e6 ); say grep /$$/,`tasklist`" perl.exe 2548 Console 1 650 +,108 K

Now we've got the list, it need to be copied to the array, which takes the other ~300MB:

C:\test>perl -E"@a=1..9e6; say grep /$$/,`tasklist`" perl.exe 2596 Console 1 937 +,388 K

When you use the range operator in the context of a for statement; it acts as an iterator, thus completely avoiding the creation of the stack-based list:

C:\test>perl -E"1 for 1..9e6; say grep /$$/,`tasklist`" perl.exe 4112 Console 1 4 +,776 K

If we just assigned the values to the array one at a time, the array would have to keep doubling in size each time it filled; in order to accommodate new values, resulting in the memory from previous resizings freed to the heap, but still needed at one instance in time and an overall memory usage of 400MB:

C:\test>perl -E"$a[$_-1]=$_ for 1..9e6; say grep /$$/,`tasklist`" perl.exe 2800 Console 1 402 +,312 K

By pre-sizing the array to its final size we save those intermediate resizings and another 100+MB:

C:\test>perl -wE"$#a=9e6; $a[$_-1]=$_ for 1..9e6; say grep /$$/,`taskl +ist`" perl.exe 4880 Console 1 292 +,196 K

Simple steps with big gains.


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".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?


Comment on Re^3: how apply large memory with perl?
Select or Download Code
Re^4: how apply large memory with perl?
by aaron_baugher (Deacon) on Aug 09, 2012 at 13:35 UTC

    Thank you, that cleared things up perfectly. I was familiar with the bit from perldata about pre-sizing large arrays for efficiency (although it describes the gain as "miniscule," which is clearly not the case here), but I assumed that meant the CPU efficiency of not requiring repeated re-allocations of memory -- the doubling you speak of. I didn't realize that those doublings were done in new memory rather than appending to what was already allocated, causing inefficiency memory-wise too. And I hadn't thought about the fact that two arrays exist at once in the first method, but it makes perfect sense.

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      I didn't realize that those doublings were done in new memory rather than appending to what was already allocated

      If the process already has sufficient memory for the doubling, and the memory immediately above the existing allocation is free, then the C-style array of pointers that forms the backbone of a Perl array may be realloc()able in-place, thereby alleviating the necessity for the previous and next sized generations to coexist. It also avoids the necessity to copy the pointers. But that's a pretty big if.

      That said, by far the biggest save comes from avoiding building big lists on the stack. For example, compare iterating an array using:

      • for
        $#a = 1e6;; say grep /$$/, `tasklist`;; perl.exe 6740 Console 1 17 +,120 K $n=1e6-1; $t=time; ++$a[ $_ ] for 0..$n; print time()-$t;; 0.242353916168213 say grep /$$/, `tasklist`;; perl.exe 6740 Console 1 41 +,312 K
      • map
        #a = 1e6;; say grep /$$/, `tasklist`;; perl.exe 4816 Console 1 17 +,128 K $n=1e6-1; $t=time; map ++$a[ $_ ], 0..$n; print time()-$t;; 0.32020902633667 say grep /$$/, `tasklist`;; perl.exe 4816 Console 1 88 +,688 K
      • But it's not as simple as just for better than map. This time using the oft-championed Perl-style foreach-style loop:
        $#a = 1e6;; say grep /$$/, `tasklist`;; perl.exe 8152 Console 1 17 +,152 K $t=time; ++$_ for @a; print time()-$t;; 0.709916114807129 say grep /$$/, `tasklist`;; perl.exe 8152 Console 1 73 +,712 K

        (in this case), The Perl foreach-style loop ends up using twice as much memory and 3 times as much cpu as the much-decried iterator-style for loop!

      When you routinely work with very large volumes of data and cpu-bound processes, rather than (typically cgi-based) IO-bound processes where 1 MB is often considered "big data"; you shall count yourself lucky the preponderance of programmers and pundits that fall into the latter camp have not yet exercised much influence on the nature of Perl.

      I revel in Perl's TIMTOWTDI, that allows me to tailor my usage to the needs of my applications; rather than being forced into the straight-jacket of the theoretical "best way" as defined by someone(s) working in unrelated fields with entirely different criteria.

      If I wanted the world of "only one good way", I'd use python.


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-08-30 16:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls