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
|