I have a script that builds a large data structure (a hash of records), and I notice that as the data set grows, it's beginning to slow down due to its large memory consumption. So now I'm trying to restrict the amount of memory it needs, by storing as minimal as necessary, and as compact as possible. The record structure is equivalent to:
$record{$key} = { count => $integer, flag => $boolean }
Populating the hash goes something like:
foreach my $item (LARGE_LIST) {
$key = property($item);
$record{$key}{count}++;
if(condition_is_true($item)) {
$record{$key}{flag} = 1;
}
}
Later I'll be only interested to extract those keys for which the flag is set, and the count is 2 or more.
Now, how (and by how much) can I restrict the memory demands more? I think an array instead of a hash for the record structure could help a little, but probably not much.
I've also thought of using a string for the record and vec, but the equivalent of the above probably won't be this simple or as fast, for example, vec prefers that the string is defined — no depending on autovivification.
Any other ideas?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.