in reply to 'reversing' a hash

I can't thing of any particular advantage for this but...

@inverted{ values %hash } = keys %hash;


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Replies are listed 'Best First'.
Re: Re: 'reversing' a hash
by Tomte (Priest) on Jun 19, 2003 at 12:01 UTC

    I couldn't resist ;-)

    use Data::Dumper; use Benchmark qw(:all); %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=> +4); cmpthese(1000000, { 'map' => sub { %ERRORS_TEXT = map { $ERRORS{$_} => $_ } keys %ERRORS; }, 'reverse' => sub { %ERRORS_TEXT = reverse %ERRORS; }, 'values' => sub { @ERRORS_TEXT{ values %ERRORS } = keys %ERRORS; }, } ); __END__ First run: Benchmark: timing 1000000 iterations of map, reverse, values... map: 27 wallclock secs (24.66 usr + 0.03 sys = 24.69 CPU) @ 40 +502.23/s (n=1000000) reverse: 10 wallclock secs ( 9.03 usr + 0.00 sys = 9.03 CPU) @ 11 +0741.97/s (n=1000000) values: 8 wallclock secs ( 7.70 usr + 0.04 sys = 7.74 CPU) @ 12 +9198.97/s (n=1000000) Rate map reverse values map 40502/s -- -63% -69% reverse 110742/s 173% -- -14% values 129199/s 219% 17% -- Second run: Benchmark: timing 1000000 iterations of map, reverse, values... map: 27 wallclock secs (24.49 usr + 0.03 sys = 24.52 CPU) @ 40 +783.03/s (n=1000000) reverse: 8 wallclock secs ( 8.54 usr + 0.02 sys = 8.56 CPU) @ 11 +6822.43/s (n=1000000) values: 7 wallclock secs ( 7.55 usr + 0.01 sys = 7.56 CPU) @ 13 +2275.13/s (n=1000000) Rate map reverse values map 40783/s -- -65% -69% reverse 116822/s 186% -- -12% values 132275/s 224% 13% -- Third run: Benchmark: timing 1000000 iterations of map, reverse, values... map: 26 wallclock secs (24.37 usr + 0.06 sys = 24.43 CPU) @ 40 +933.28/s (n=1000000) reverse: 11 wallclock secs ( 8.63 usr + 0.03 sys = 8.66 CPU) @ 11 +5473.44/s (n=1000000) values: 9 wallclock secs ( 7.57 usr + 0.01 sys = 7.58 CPU) @ 13 +1926.12/s (n=1000000) Rate map reverse values map 40933/s -- -65% -69% reverse 115473/s 182% -- -12% values 131926/s 222% 14% --

    Looks like I could gain a wee bit of speedup using your code, if it would matter, which it doesn't in my particular case; for the loss of readability, though IMHO :)

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      Wow! A whole 1.3 microseconds saved. And to think I didn't see any merit in it:)

      On a slightly serious note, I think a novice perler might actually find the "values become keys and keys become values" slightly more obvious than the reverse method, which relies on a pretty good understanding of how hashes work in list contexts, on their first viewing, but once you understand that, the presence of the big clue, R-E-V-E-R-S-E does make it pretty clear:)


      Examine what is said, not who speaks.
      "Efficiency is intelligent laziness." -David Dunham
      "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller