Re: Transform hash key?



Uri Guttman <uri@xxxxxxxxxxxxxxx> wrote in
news:x7wtggtf56.fsf@xxxxxxxxxxxxxxxx:

>>>>>> "ASU" == A Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx> writes:
>
> ASU> my %h = map { $_ => 1 } ('aaa' .. 'zzz');
>
> ASU> cmpthese -1, {
> ASU> map => sub { %h = map { uc $_ => $h{$_} } keys %h },
> ASU> for => sub { $h{uc $_} = delete $h{$_} for keys %h; },
> ASU> };
>
> this benchmark has a common problem in that you don't set up the hash
> the same way for each run.

I actually thought about this before posting.

> the first time you call either sub, the hash will have only
> upper case keys. now i doubt that uc will be faster or slower
> given any mix of input case

Which is why I decided not worry about it (in this case).

> but it still is wrong.

I am not sure it is that wrong.

> if the transform were something whose speed was data dependent,
> this benchmark would be very bogus.

Absolutely.

> the map one could be fixed just by assigning it to a different hash.
> the for one would need a fresh hash each time as it modifies it in
> place. so you would need to do the same work for each one and my best
> idea would be to copy the %h to %h2 inside each sub and use that as
> the input (and you can use it for the output of the map. also choosing
> a different output hash for the map could affect speed as well.
>
> this is just a small lesson on the difficulty of properly benchmarking
> apples and apples.

Do you meam I should use:

#!/usr/bin/perl

use warnings;
use strict;

use Benchmark qw( cmpthese );

my %h = map { $_ => 1 } ('aaa' .. 'zzz');

cmpthese -1, {
map => sub { my %h2 = %h; %h2 = map { uc $_ => $h{$_} } keys %h },
for => sub { my %h2 = %h; $h2{uc $_} = delete $h2{$_} for keys %h2; },
};
__END__

D:\Home\asu1\UseNet\clpmisc> s.pl
Rate map for
map 7.31/s -- -39%
for 12.0/s 64% --

Or, should I omit the my %h2 = %h in the 'map' version?

D:\Home\asu1\UseNet\clpmisc> s.pl
Rate for map
for 12.1/s -- -3%
map 12.4/s 3% --

I don't think the latter version is fair to 'for' though.

Sinan

--
A. Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
.



Relevant Pages

  • Re: Transform hash key?
    ... ASU> cmpthese -1, { ... this benchmark has a common problem in that you don't set up the hash ... transform were something whose speed was data dependent, this benchmark ... the map one could be fixed just by assigning it to a different hash. ...
    (comp.lang.perl.misc)
  • Re: Transform hash key?
    ... >> this benchmark has a common problem in that you don't set up the hash ... ASU> I actually thought about this before posting. ... >> the map one could be fixed just by assigning it to a different hash. ... ASU> use Benchmark qw; ...
    (comp.lang.perl.misc)
  • Re: Cryptographic Functions
    ... how does the pass phrase map to ... The passphrase should be derived using a KDF. ... Also, assuming that a strong Hash Function is being used, then it is ... Information Assurance Certification Review Board ...
    (Pen-Test)
  • Re: Two questions about split and map in a cookbook recipe
    ... home) to produce a record structure from items in a text file. ... what is the map doing in the last line, ... the newline since the. ... If a list is assigned to a hash variable (in this ...
    (perl.beginners)
  • Re: Cryptographic Functions
    ... how does the pass phrase map to ... The passphrase should be derived using a KDF. ... Also, assuming that a strong Hash Function is being used, then it is ... When a passphrase is used a key in symetric cryptography, ...
    (Pen-Test)