http://www.perlmonks.org?node_id=11125807


in reply to Re^3: using ref to hash of hash effectively
in thread using ref to hash of hash effectively

In terms of aesthetics, my feeling is "each to his own". I used to use map in void context. I have no problem reading code like that. I no longer use it, but for completely different reasons (see below).

The performance hit was resolved in 5.8.1 (see "perl581delta: Miscellaneous Enhancements"). If you're coding to 5.8.0 or earlier, you should avoid map in void context; otherwise, the performance issue is moot.

Quite a few years ago, I benchmarked map against for. I was surprised at how much faster for was. That's the reason I would now choose

for (keys %{ $weapons_ref }) { ... }

over

map { ... } keys %{ $weapons_ref };

See Benchmark and run your own tests if you want.

— Ken

Replies are listed 'Best First'.
Re^5: using ref to hash of hash effectively
by alexander_lunev (Pilgrim) on Dec 27, 2020 at 19:18 UTC

    Thank you for this. I've done this benchmark:

    use strict; use Benchmark qw(:all); my $weapons_ref = { dagger => { cost => 8, damage => 4, armor => 0 }, shortsword => { cost => 10, damage => 5, armor => 0 }, warhammer => { cost => 25, damage => 6, armor => 0 }, longsword => { cost => 40, damage => 7, armor => 0 }, greataxe => { cost => 74, damage => 8, armor => 0 }, }; my @sum_fields = qw/cost damage armor/; my $count; cmpthese($count, { 'MapSub' => sub { my $sum_result; map { my $name = $_; map { $sum_result->{$_} += $weapons_ref->{$name}->{$_} } @sum_ +fields } keys %{ $weapons_ref }; }, 'ForSub' => sub { my $sum_result; foreach my $name (keys %{ $weapons_ref }) { foreach my $field (@sum_fields) { $sum_result->{$field} += $weapons_ref->{$name}->{$field}; } } }, });

    And here's result:

    Rate MapSub ForSub MapSub 197098/s -- -33% ForSub 294627/s 49% --

    map is a bit slower, yes, but not critical. Anyway, thank you again, I will remember this if have to do code optimization.