tadman has asked for the wisdom of the Perl Monks concerning the following question:
Which I thought would be far to easy, since it didn't harness the Power of Map. So, instead, I supposed that this might be faster:sub AssembleByHash { my ($array_ref) = @_; my (%result); foreach my $row (@$array_ref) { $result{$row->[0]} = $row->[1]; } return \%result; }
After all, it's quick and to the point. When applying Benchmark to validate my theory, though, I got the following result using a 10_000 row test-table and 100 iterations:sub AssembleByArray { my ($array_ref) = @_; my (%result); %result = map { @$_ } @$array_ref; return \%result; }
So, I can only suppose that the temporary array created by the map call is killing performance. Or, am I missing something?Benchmark: timing 10 iterations of AssembleByArray, AssembleByHash... AssembleByArray: 10 wallclock secs ( 9.64 usr + 0.04 sys = 9.68 CPU) + @ 1.03/s (n=10) AssembleByHash: 1 wallclock secs ( 1.31 usr + 0.00 sys = 1.31 CPU) +@ 7.63/s (n=10) Rate AssembleByArray AssembleByHash AssembleByArray 1.03/s -- -86% AssembleByHash 7.63/s 639% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Multi-Dimensional Array to Hash Conversion Optimization
by MeowChow (Vicar) on Apr 03, 2001 at 20:21 UTC | |
|
(boo) Re: Multi-Dimensional Array to Hash Conversion Optimization
by boo_radley (Parson) on Apr 03, 2001 at 20:39 UTC | |
by tadman (Prior) on Apr 03, 2001 at 20:42 UTC | |
by boo_radley (Parson) on Apr 03, 2001 at 20:47 UTC | |
by tadman (Prior) on Apr 03, 2001 at 21:00 UTC | |
by boo_radley (Parson) on Apr 03, 2001 at 21:11 UTC |