Re: Transform hash key?
- From: "A. Sinan Unur" <1usa@xxxxxxxxxxxxxxxxxxx>
- Date: Tue, 31 Jan 2006 21:34:53 GMT
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
.
- Follow-Ups:
- Re: Transform hash key?
- From: Uri Guttman
- Re: Transform hash key?
- References:
- Transform hash key?
- From: MSG
- Re: Transform hash key?
- From: A. Sinan Unur
- Re: Transform hash key?
- From: Uri Guttman
- Re: Transform hash key?
- From: A. Sinan Unur
- Re: Transform hash key?
- From: Uri Guttman
- Transform hash key?
- Prev by Date: Re: split the sequences
- Next by Date: Trouble sending an email with Mailer module
- Previous by thread: Re: Transform hash key?
- Next by thread: Re: Transform hash key?
- Index(es):
Relevant Pages
|