P is for Practical | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Before you complain about the speed of a hash, benchmark it. The difference between a hash and an array shouldn't be much. I doubt a sparse array would be any faster. Your program is already heavily invested in the performance of hashes. Most objects use hashes as their underlying data structure. Every reference to a package variable results in multiple hash lookups (I think). And every call to a subroutine results in multiple hash lookups (I think).
The benefit of arrays come from knowing exactly where to look into memory for a given element knowing only a base address and the element index. That becomes impossible if some of the elements are missing. Now, there could be something more efficient data structure than a hash for your needs, but accessing it would require calling a subroutine/method. I believe that results in a hash lookup to find the code pointer associated with the subroutine name, so you're worse off than you started. On the plus side, arrays use the most efficient of two kinds of undef by default if I read what follows correctly. Instead of creating a new undef SV, uninitialized array elements refer to a global undef constant. That means only sizeof(void*) bytes are wasted per unused slot instead of sizeof(void*) + sizeof(SV).
In reply to Re^3: Could we save the memory occupied by "undef" in an array?
by ikegami
|
|