Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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 :)

5.008008 c -10 2 ** 8 == 256 Rate simap2 simap2 2721/s simap 2770/s mmap 3196/s imap 3505/s fforc 3981/s sffor 4335/s sfforc 4535/s ffor 4838/s ifor 5027/s sifor 5798/s smap 8123/s slic 9384/s 5.008008 c -10 2 ** 16 == 65536 Rate simap2 simap2 3.82/s simap 3.83/s mmap 4.53/s imap 5.26/s sffor 7.86/s sfforc 7.89/s fforc 8.09/s smap 8.46/s sifor 8.68/s ffor 8.83/s ifor 8.93/s slic 9.47/s 5.008008 c -10 2 ** 18 == 2621443 Rate simap2 simap2 0.810/s simap 0.825/s mmap 0.979/s imap 1.18/s smap 1.68/s sfforc 1.75/s sffor 1.80/s sifor 1.90/s fforc 1.90/s slic 1.93/s ffor 2.08/s ifor 2.10/s
5.008009 c -10 2 ** 8 == 256 Rate simap simap 1767/s simap2 1775/s mmap 2017/s imap 2183/s fforc 2980/s sffor 3326/s ffor 3389/s ifor 3565/s sfforc 4920/s sifor 6445/s smap 8908/s slic 9698/s 5.008009 c -10 2 ** 16 == 65536 Rate mmap mmap 2.36/s simap2 2.37/s simap 2.37/s imap 2.51/s sffor 5.77/s fforc 6.03/s ffor 6.42/s ifor 6.53/s sfforc 7.95/s smap 8.75/s sifor 8.83/s slic 9.82/s 5.008009 c -10 2 ** 18 == 2621442 **too few iter Rate imap imap 0.187/s mmap 0.259/s simap2 0.298/s simap 0.302/s sffor 1.44/s fforc 1.50/s ffor 1.60/s ifor 1.63/s smap 1.68/s sfforc 1.75/s slic 1.91/s sifor 1.92/s
5.012002 c -10 2 ** 8 == 256 Rate mmap mmap 1364/s imap 1456/s simap 1734/s simap2 1742/s fforc 3086/s sffor 3224/s ffor 3479/s ifor 3551/s sfforc 5073/s sifor 6343/s smap 8775/s slic 9969/s 5.012002 c -10 2 ** 16 == 65536 Rate mmap mmap 1.99/s imap 2.01/s simap 3.10/s simap2 3.11/s sffor 6.63/s fforc 6.81/s ffor 7.27/s ifor 7.35/s sfforc 9.58/s sifor 10.5/s smap 10.6/s slic 12.1/s 5.012002 c -10 2 ** 18 == 2621442 **too few iter Rate imap imap 0.211/s mmap 0.242/s simap2 0.406/s simap 0.409/s sffor 1.67/s fforc 1.75/s ffor 1.85/s ifor 1.89/s sfforc 2.11/s smap 2.11/s sifor 2.32/s slic 2.46/s
5.014001 c -10 2 ** 8 == 256 Rate mmap mmap 2056/s imap 2163/s simap 2167/s simap2 2191/s sfforc 3280/s fforc 3339/s sffor 3799/s ffor 3854/s sifor 3880/s ifor 3897/s smap 4659/s slic 4748/s 5.014001 c -10 2 ** 16 == 65536 Rate imap imap 2.07/s mmap 2.44/s simap2 2.59/s simap 2.60/s sfforc 7.01/s fforc 7.13/s sifor 7.58/s sffor 7.63/s slic 7.72/s smap 7.74/s ffor 7.75/s ifor 7.81/s 5.014001 c -10 2 ** 18 == 2621440 **too few iter **too few iter **too few iter Rate imap imap 0.156/s mmap 0.173/s simap2 0.177/s simap 0.177/s sfforc 1.62/s fforc 1.63/s slic 1.67/s smap 1.67/s sifor 1.75/s sffor 1.76/s ffor 1.77/s ifor 1.79/s
5.016001 c -10 2 ** 8 == 256 Rate mmap mmap 2227/s simap 2282/s simap2 2283/s imap 2321/s sfforc 3331/s fforc 3382/s sffor 3484/s ffor 3575/s sifor 3772/s ifor 3831/s smap 4733/s slic 4829/s 5.016001 c -10 2 ** 16 == 65536 Rate imap imap 2.06/s mmap 2.48/s simap 2.59/s simap2 2.65/s sfforc 6.90/s smap 6.92/s slic 7.01/s sffor 7.07/s fforc 7.12/s ffor 7.33/s sifor 7.43/s ifor 7.69/s 5.016001 c -10 2 ** 18 == 2621440 **too few iter **too few iter **too few iter Rate imap imap 0.156/s mmap 0.176/s simap2 0.177/s simap 0.178/s smap 1.45/s slic 1.47/s sfforc 1.60/s fforc 1.63/s sffor 1.65/s ffor 1.68/s sifor 1.70/s ifor 1.75/s


Comment on Re^2: better array to hash conversion
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008370]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-09-03 07:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls