On Sunday, January 6, 2002, at 10:21 AM, James Edward Gray II wrote: > Is it a big gain to pre-extend the data structures? With how many > entries? >
It's not usually that much of a gain, but you should benchmark it to make sure. The allocation scheme perl uses (for both arrays and hashes) is to grow geometrically. It doubles the allocation, then doubles again when you run out of space, then doubles again next time, and so on. If you remember much about geometric growth, you know it grows very quickly, so you shouldn't need more than a handful of actual allocation passes. > Does a newly created array/hash have a default capacity? Undoubtedly, but I don't know what it is. ;-) You could look in the source, but it's bound to vary depending on what version of perl you're using. > > Are stacks (arrays, using push and pop) a performance strain because of > the constant size changes? No - perl doesn't try to reclaim memory that it's used before. So if you're just pushing & popping, you won't be doing any new memory allocation unless the structures are tending toward growth in the long run. If you're using a circular array (pushing & shifting, or unshifting & popping), then you might have some memory issues, but perl might actually handle these surprisingly well too. Your best strategy is to benchmark it, of course. -Ken
