Re: Transform hash key?




A. Sinan Unur wrote:
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> use Benchmark qw( cmpthese );
>
> my %h = map { $_ => 1 } ('aaa' .. 'zzz');
>
> cmpthese -1, {
> map => sub { %h = map { uc $_ => $h{$_} } keys %h },
> for => sub { $h{uc $_} = delete $h{$_} for keys %h; },
> };
>
> D:\Home\asu1\UseNet\clpmisc> s.pl
> Rate map for
> map 12.4/s -- -60%
> for 31.5/s 153% --
>
> So the for loop takes about 1/3 of the time the map takes, and
> this ratio remains roughly constant when the number of keys is
> increased.
>
> Nice.

Would it be possible to alias the keys and edit in-place? It would be
easy to do so with the values, but I'm not sure about the keys.

Just wondering.

Tks.
-jp

.



Relevant Pages

  • Re: How to find regex at specific location on line
    ... benchmark complained about too few iterations. ... use Benchmark qw(:hireswallclock cmpthese timethese); ... timing 10000000 iterations of plain_regex, plain_substr, ...
    (perl.beginners)
  • single-character-sets (was: Re: Strange behavior by regex with variable)
    ... let's see what Benchmark says: ... $ perl -MBenchmark=cmpthese -e' ... cmpthese -10, { ... Rate cc lit ...
    (comp.lang.perl.misc)
  • Re: initialize a hash
    ... use strict; ... use Benchmark qw(:hireswallclock cmpthese); ... Rate mapB mapE list for range empty ... to your benchmark shows it as fast as "range" on my machine, ...
    (comp.lang.perl.misc)
  • Re: Optimizing a conditional
    ... conditional in Perl. ... Here is a silly example to show you how you would benchmark something ... use Benchmark qw(cmpthese); ... first_false => sub { ...
    (comp.lang.perl.misc)
  • Re: Help: Cannot acquire data by split
    ... Feel free to benchmark. ... I'm betting that be it iterations of ... slightly faster than the regex. ... cmpthese $t; ...
    (comp.lang.perl.misc)