in reply to Re^2: Architectural question...
in thread Architectural question...
I don't get this code?
You take the keys in turn and break them into their constituant parts.
while(my($key,$val) = each(%ret)) { my @parts = split(/\./, $key); my $id = ''; foreach my $add (@parts) {
Then you iterate over those parts and concatenate them back together, piece by piece, into $id.
$id .= $add;
And then, if $id (so far) doesn't match $key, you skip to the next iteration
if($id eq $key) { next; }
Which means you'll never reach this code, until you've completely re-built $id to match $key.
if(!$ret{$id}{'count'}) { $ret{$id}{'count'} = 0; $ret{$id}{'head_count'} = 'head_count'; $ret{$id}{'cat_count'} = 'cat_count'; $ret{$id}{'subcat_count'} = 'subcat_count'; } $ret{$id}{'count'} += $ret{$key}{'count'}; $id .= '.'; } }
Which, unless I'm missing something (quite possible), there is no need for split or the for loop as this would do the same thing:
while(my($key,$val) = each(%ret)) { $id = $key; if(!$ret{$id}{'count'}) { $ret{$id}{'count'} = 0; $ret{$id}{'head_count'} = 'head_count'; $ret{$id}{'cat_count'} = 'cat_count'; $ret{$id}{'subcat_count'} = 'subcat_count'; } ## Though this doesn't do much? $ret{$id}{'count'} += $ret{$key}{'count'}; }
What did I miss?
BTW. It won't help much towards your performance goal, but you can save a bit of memory by changing:
my @keys = sort keys %cats; my $cnt = scalar @keys; ... @keys = keys %ids; $cnt = scalar @keys; ... @keys = keys %ret; $cnt = scalar @keys;
to
my $cnt = keys %cat; ... $cnt = keys %ids; ... $cnt = keys %ret;
which does the same thing without copying all the keys to an array or sorting one set of them for no reason. Actually, if your low on memory, avoiding that sort and the (re-)allocation of those arrays might help your performance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Architectural question...
by devnul (Monk) on Jul 05, 2005 at 17:50 UTC |