Are your events and attributes numerical, or can they proceeds into number?

If so, then using the hash as you are is definitely inefficient. Your 'data' above only needs 1MB to store data, but because you're using a string, that's more perl needs to keep in memory.

I'd suggest two approaches. I'll assume that event numbers are *not* sparse (that is, you most likely generate consecutive event numbers from your logging program). If the attribute numbers are also not sparse, then use a flatted 2d-to-1d array:

sub getArray { my ( $event, $attrib ) = @_; return $array[ $event*MAX_ATTRIBS + $attrib ]; } sub putArray { my ( $event, $attrib, $value ) = @_; $array[ $event*MAX_ATTRIBS + $attrib ] = $value; }
(Writing out results as 'event|attrib' can be done in the last step).

Alternatively, if the attributes are sparse, use a hash of lists, hashing on the attribute...

sub putArray { my( $event, $attrib, $value ) = @_; $hash{ $attrib } ||= []; # create if not def $hash{ $attrib }->[ $event ] = $value; } sub getArray { my( $event, $attrib ) = @_; return $hash{ $attrib }->[ $event ]; }
Mind you, in both cases, if what you are putting in the array is 'large' (as large as a few-character text string), then in your 1000x1000 case you'll be grinding memory. If you have sparse attributes, you probably can get away with larger strings until this happens. But in either case, you will hit something with large events/attributes values and meaningful data content. At which point I would resort to temporary files, and drop back to the 'flattened array' approach:
sub putArray { my ( $event, $attrib, $value ) = @_; open FILE, '>' . sprintf( "%06d", $event ) . '-' . sprintf( "%06d", $attrib ) or die $!; print FILE $value; close FILE; } sub getArray { my ( $event, $attrib ) = @_; return if !( -e sprintf( "%06d", $event ) . '-' . sprintf( "%06d", $attrib ) ); open FILE, '<' . sprintf( "%06d", $event ) . '-' . sprintf( "%06d", $attrib ) or die $!; my $value = <FILE>; close FILE; return $value; }
Yes, it will be slow, but you will not be thrashing memory as your events and attributes expand.


Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

In reply to Re: limiting associative array memory usage by Masem
in thread limiting associative array memory usage by FreckledAvenger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.