Re: Transform hash key?



>>>>> "ASU" == A Sinan Unur <1usa@xxxxxxxxxxxxxxxxxxx> writes:

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

>> this benchmark has a common problem in that you don't set up the hash
>> the same way for each run.

ASU> 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

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

still not a good idea to not deal with it. it may have worked ok for
this case but it could lead you or someone reading this to make similar
broken benchmarks.

>> 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.

ASU> Do you meam I should use:

ASU> #!/usr/bin/perl

ASU> use warnings;
ASU> use strict;

ASU> use Benchmark qw( cmpthese );

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

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

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

that would be a better and more truthful benchmark. and you should add a
control case which just does the hash copy so you could subtract that
time and get a more accurate comparison of the real code under test.

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

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

no, that is unfair.

uri

--
Uri Guttman ------ uri@xxxxxxxxxxxxxxx -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
.



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 ... > this benchmark would be very bogus. ... > the map one could be fixed just by assigning it to a different hash. ... use Benchmark qw(cmpthese); ...
    (comp.lang.perl.misc)