Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: quickly create reference to boring hash.

by starbolin (Hermit)
on May 14, 2008 at 04:06 UTC ( [id://686449]=note: print w/replies, xml ) Need Help??


in reply to Re: quickly create reference to boring hash.
in thread quickly create reference to boring hash.

Except my previous post is wrong. Perl hashes are so dang slick that they beat all my hackery with arrays and pseudo-hashes. In deference to those who say CPU cycles DO NOT MATTER!, the difference is small enough that the OP could chose the method that is the most readable.

#!/usr/bin/perl require 5.8.8; use strict; use fields; use Benchmark 'cmpthese'; use Hash::Util 'lock_keys'; use Data::Dumper; my @range = ('a'..'z'); # Define a perl 5.8 pseudo hash my $inputs = fields::phash(\@range, [ map { [1,1] } @range ]); $inputs->{c} = [ 0, 1 ]; $inputs->{f} = [ 2, 1 ]; $inputs->{g} = [ 0, 1 ]; my $phash = sub { map $inputs->{$_}, @range}; # Define an AofA with index conversion my @iRange = (0..25); my @array = map { [1, 1] } @iRange; my $aoa = sub { map $array[ ord($_)-97 ], @range }; # Define a hash my %HofA = map { $_=>[1, 1] } @range; my $hoa = sub { map $HofA{$_}, @range }; # Define a locked hash my %LHofA = map { $_=>[1, 1] } @range; lock_keys(%LHofA, @range); my $lhoa = sub { map $LHofA{$_}, @range }; cmpthese ( 10000, { pseudohash => $phash, Hash_of_Arrays => $hoa, Locked_Hash => $lhoa, Array_of_Arrays => $aoa, }); __END__ perl src/symarray.pl [ +8:54pm] (warning: too few iterations for a reliable count) (warning: too few iterations for a reliable count) Rate pseudohash Array_of_Arrays Hash_of_Arrays L +ocked_Hash pseudohash 19104/s -- -12% -36% + -37% Array_of_Arrays 21695/s 14% -- -27% + -29% Hash_of_Arrays 29767/s 56% 37% -- + -2% Locked_Hash 30476/s 60% 40% 2% + --


s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

Replies are listed 'Best First'.
Re^3: quickly create reference to boring hash.
by pemungkah (Priest) on May 14, 2008 at 22:05 UTC
    Pseudo-hashes have been deprecated; they never worked really well in the first place, and don't work at all in newer Perls. Using them will lock your code into a particular version of Perl, which is of course inadvisable.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-03-28 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found