One thing I see is that $hash_ref seems to be constant, so you can compute the list of keys once instead of each time: @keys = keys %$hash_ref; and for my $key (@keys)
And if several $a values can share $b sub values, you can keep the subsums in cache to avoid recomputing them (untested because you didn't provide sample data):
Using the orcish manoeuver, List::Util, and a sliceuse List::Util qw( sum ); my %cache; my @keys = keys %$hash_ref; foreach my $a_ (@{ $array_ref }) { my $i = 0; foreach my $b_ (split('-', $a_)) { $i += $cache{$b_} //= sum map { $_->{value} } @{ $foo->{$b_} } +{@keys}; # Use the cached value or compute the sum } push @{ $bar->{$i} }, $a_; }
I suppose the name are only because this is an example? Even in a example I would avoid $a and $b, (even more so when using List::Util) because of their special meaning. (Edit) And I avoid single char names as a general rule
Edit $hash_ref->{$b_ } should have been $foo->{$b_}. Thanks kcott
In reply to Re: Optimize a perl block
by Eily
in thread Optimize a perl block
by IruP
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |