in reply to limiting associative array memory usage
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:
(Writing out results as 'event|attrib' can be done in the last step).sub getArray { my ( $event, $attrib ) = @_; return $array[ $event*MAX_ATTRIBS + $attrib ]; } sub putArray { my ( $event, $attrib, $value ) = @_; $array[ $event*MAX_ATTRIBS + $attrib ] = $value; }
Alternatively, if the attributes are sparse, use a hash of lists, hashing on the attribute...
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 ) = @_; $hash{ $attrib } ||= []; # create if not def $hash{ $attrib }->[ $event ] = $value; } sub getArray { my( $event, $attrib ) = @_; return $hash{ $attrib }->[ $event ]; }
Yes, it will be slow, but you will not be thrashing memory as your events and attributes expand.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; }
|
|---|