http://qs321.pair.com?node_id=175267


in reply to scoping large arrays - newbie Q

Generally, from what I've seen so far Perl is rather good at managing memory efficiently and quickly.

Having the array persistent may speed things up if the function is being called ten thousands of times, but it also is an invitation to hogging memory, not to mention global variables just generally tend to lead to headaches. Creating a lexically scoped array a new slows things down a wee notch, but keeps the code clean and the memory footprint lean.

We're only talking 20,000 bytes of data here, that's not something I'd consider much of an array.

Two options you have if you really need the performance is passing around references instead, to keep things properly scoped without the overhead of array allocation; or using a closure like so:

{ my @not_global; sub operate_on_not_global { @not_global = whatever(); } }

Bottome line: personally, I always take the defensive approach - I can always add dirty tricks later if my clean code is not fast enough, but if I start out dirty at the scratchpad, I'll never get a handle on things and end up with a big ball of mud.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: scoping large arrays - newbie Q
by moof1138 (Curate) on Jun 18, 2002 at 05:08 UTC
    Thank you Screamer, that confirms what I had been getting the nagging feeling about. I had been leery of the globally scoped vars, since I would not consider using them in other languages. While I was wondering about the question in general, after I though about it for a minute, I realized that performance is not really an issue for this project - it is just an AIM bot, it winds up needing to sleep at times to keep it from hitting the AIM flood control limits anyway. I just reworked it scoping all arrays proper to their functions, and I don't feel any performance difference, really.