Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: better array to hash conversion

by Anonymous Monk
on Dec 11, 2012 at 19:09 UTC ( #1008370=note: print w/replies, xml ) Need Help??


in reply to Re: better array to hash conversion
in thread better array to hash conversion

presize/preallocate buckets , change the number of elements, change the perl version, and the winner changes :)

funny bench :)

#!/usr/bin/perl -- use strict; use warnings; use Benchmark qw/ cmpthese /; #~ my $cmptime = -abs(shift); #~ my $cmptime = -3; my $cmptime = -10; printf "%10s cmptime %s\n", $], $cmptime; my @array; for my $power ( 8, 16,18 ) { #~ for my $power ( 8, 16, 20, 24 ){ @array = 1 .. ( 2**$power ); print "2 ** $power == @{[int @array ]}\n"; cmpthese( $cmptime, { map { ; $_ => __PACKAGE__->can( $_ ) } qw/ ffor fforc ifor imap mmap sffor sfforc sifor simap slic smap simap2 / } ); } exit; sub mmap { my %hash = map { ; $array[$_] => $_ } 0 .. $#array; (); } sub imap { my $ix = 0; my %hash = map { ; $_ => $ix++ } @array; (); } sub slic { my %hash; @hash{@array} = 0 .. $#array; (); } sub ffor { my %hash; for my $ix ( 0 .. $#array ) { $hash{ $array[$ix] } = $ix; } (); } sub fforc { my %hash; for( my $ix = 0 ; $ix < @array ; $ix++ ) { $hash{ $array[$ix] } = +$ix; } (); } sub ifor { my %hash; my $ix = 0; $hash{$_} = $ix++ for @array; (); } ## preSize sub sffor { my %hash; keys( %hash ) = int @array; for my $ix ( 0 .. $#array ) { $hash{ $array[$ix] } = $ix; } (); } sub sfforc { my %hash; keys( %hash ) = int @array; for( my $ix = 0 ; $ix < @array ; $ix++ ) { $hash{ $array[$ix] } = +$ix; } (); } sub sifor { my %hash; my $ix = 0; keys( %hash ) = int @array; $hash{$_} = $ix++ for @array; (); } sub smap { my %hash; keys( %hash ) = int @array; @hash{@array} = 0 .. $#array; (); } sub simap { my $i = 0; my %hash; keys( %hash ) = int @array; %hash = map { $_ => $i++ } @array; (); } sub simap2 { my $i = 0; my %hash; keys( %hash ) = int @array; %hash = map(( $_ => $i++ ), @array); (); } __END__

funny results :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008370]
help
Chatterbox?
[Corion]: marto: Well, I think they go a tour every two years and I think it's hard to even get a connection with the crowd at a 20k people concert... But maybe after this time I'll stop too ;)
[Corion]: I still have to see the Pet Shop Boys live before they stop touring at all
[marto]: yeah, I think that as a group creatively they're done. I can understand how it'd be hard to stop the process, album/tour, album/tour, if that's pretty much all you've ever done :)
[marto]: Corion yes I saw them Pandemonium_Tour
[Corion]: marto: Yeah, and I doubt that they'll ever get back to something like Violator/Songs of Faith and Devotion - it would either alienate their "regular" crowd, or be "too much Violator" ;)
LanX needs to see Freddy Mercury'
[Corion]: marto: Yeah, I have that on DVD even, and I missed them last year because I was in London at the time ironically ;)
[marto]: put me in the mood to listen to this now :)
[Corion]: LanX: Well, grab a shovel :)
[Corion]: Kraftwerk also are still touring (well, only Ralf Hütter and Florian Schneider), but I'm too tight fisted to spend EUR 150+ for standing around two hours ;)

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2017-03-24 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (301 votes). Check out past polls.