in reply to Am I developing poor style?
I haven't benchmarked this so don't know how it compares to yours. I'd guess it'd be faster since yours has to build up a code ref then get rid of it, etc. And sum is implemented in C, so it's fast.use List::Util qw/sum/; $header{"Number of Drills"} = sum map $_->count, map { values %{ $_->tool } } @drill_arr;
Update: okay, benchmarked them:
Results:timethese(-6, { 'sum' => sub { my $n = sum map $_->{count}, map { values %{ $_->{tool} } } @drill_arr; }, 'sub' => sub { my $n = &{ sub { my $ret; foreach my $l ( @drill_arr ) { foreach ( values %{$l->{tool}} ) { $ret += $_->{count}; } } return $ret; } }; }, });
So using sum is lots faster, apparently.Benchmark: running sub, sum, each for at least 6 CPU seconds... sub: 7 wallclock secs ( 6.00 usr + 0.00 sys = 6.00 CPU) @ 15 +883.17/s (n=95299) sum: 6 wallclock secs ( 6.02 usr + 0.00 sys = 6.02 CPU) @ 40 +993.85/s (n=246783)
Note however that I'm not using method calls; I just set up a dummy array holding hashes of hashes that I'm using.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(tye)Re3: Am I developing poor style?
by tye (Sage) on Jan 17, 2001 at 00:56 UTC |