Clear questions and runnable code
get the best and fastest answer
Re^3: how apply large memory with perl?by BrowserUk (Pope)
|on Aug 09, 2012 at 11:02 UTC||Need Help??|
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":
Now we've got the list, it need to be copied to the array, which takes the other ~300MB:
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:
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:
By pre-sizing the array to its final size we save those intermediate resizings and another 100+MB:
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.