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.


In reply to Re: Re: 'reversing' a hash by Tomte
in thread 'reversing' a hash by Tomte

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.