Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
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]: You'll have to look somewhere esoteric for that. Maybe some tied variable or special dualvar can also trigger that. But it's certainly not a common occurrence
[Corion]: And on 5.20, the following also outputs no find:perl -wle 'for my $x ("\x{2000}".."\ x{1fffff}") { if( $x && ! length $x ) { warn qq(<$x>); warn length $x; die } }'
[Corion]: (this time on Unix)
[hippo]: Understood. I'll have to go through the code and see if it's doing anything fancy with ties, dual-vars or non-scalars. In the end, it's probably a bug though.
[Corion]: Aaah - you should be able to do this with overload, but I would hit somebody really hard if they constructed objects that are true but the empty string, and you not knowing about the domain knowledge where this makes sense

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2017-07-27 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (413 votes). Check out past polls.