Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Check whether your hash keys have random order

by liz (Monsignor)
on Sep 08, 2003 at 09:38 UTC ( [id://289729]=CUFP: print w/replies, xml ) Need Help??

The subroutine is_hash_randomized() allows you to check whether the hash keys in your Perl have a random or repeatable order (as returned by each or keys. By default, in Perl 5.8.1, the order of hash keys is no longer repeatable: it will be different for each run of Perl.

The subroutine returns either a defined or an undefined value. If an undefined value is returned, then the order of hash keys is not repeatable. If a defined value if returned, it indicates the hash seed that was used to generate, and thereby order, the hash keys.

When called with a Perl < 5.8.1, the value "0" is always returned. Please note though, that the same value only guarantees repeatable hash key order only for a given version of Perl, not for different Perl versions.

Update:
Ok, that's all pretty forgettable. Perl 5.8.1 will have the the hash_seed() subroutine in the Hash::Util module. From the new pod:

hash_seed
my $hash_seed = hash_seed();

hash_seed() returns the seed number used to randomise hash ordering. Zero means the "traditional" random hash ordering, non-zero means the new even more random hash ordering introduced in Perl 5.8.1.

It should be noted that this routine has slightly different semantics, as you can not discover whether you would or would not got different hash key orders for different runs of Perl. But it doesn't need to do any strange things, it's going directly at the Perl internals.

sub is_hash_randomized { # We don't have any random sequences yet, as if seed "0" return 0 if $] < 5.008001; # Get the -Accflags= settings using this Perl executor open my $handle, "$^X -V:ccflags |" or die "Could not execute $^X -V:ccflags: $!\n"; my $ccflags = <$handle>; close $handle; # Return repeatable default if random sequences compiled out # Return random if switch settings reversed and environment variable s +et # Return whatever the environment variable indicates return 0 if $ccflags =~ m#-DNO_HASH_SEED#; return if $ccflags =~ m#-DUSE_HASH_SEED_EXPLICIT# and exists $ENV{PERL_HASH_SEED}; $ENV{PERL_HASH_SEED}; } #is_hash_randomized

Replies are listed 'Best First'.
•Re: Check whether your hash keys have random order
by merlyn (Sage) on Sep 08, 2003 at 14:00 UTC
      I was trying to save memory by not having to load Config.pm. But because of your remark, I decided to do a Benchmark:
      Benchmark: timing 1000 iterations of open, use...
       open: 38 wallclock secs ( 2.62 usr  0.00 sys + 16.14 cusr 12.96 csys = 31.72 CPU) @ 381.68/s (n=1000)
        use: 14 wallclock secs (13.15 usr +  0.00 sys = 13.15 CPU) @ 76.05/s (n=1000)
      

      This surprised me a lot!. The fork() approach with open() seems to be 5 times as fast as loading Config.pm!

      Alas, I think I stumbled upon a bug / feature / problem of Benchmark: apparently, only "usr" CPU is taken into account when calculating the number of runs/second, and the "usr" CPU is of course a lot less than with fork/open than it is with use.

      Still, the fork/open approach only takes 2.5 times as much CPU as loading Config.pm. I wonder if that is a testament of the efficiency of fork(), or the slowness of Config.pm. ;-)

      The code:

      use Benchmark; timethese( 1000, { open => sub { open my $handle, $^X.' -V:ccflags |'; my $ccflags = <$handle>; delete $INC{'Config.pm'}; }, use => sub { require Config; Config->import; my $ccflags = $Config{ccflags}; delete $INC{'Config.pm'}; }, } );

      Liz

        The use version creates a tied perl hash, employing caching ... while the other version doesn't bother to do that and simply matches on a scalar.
        perl -d:Trace -V:ccflags >> Config.pm:1012: foreach(@_){ >> Config.pm:1013: config_re($_), next if /\W/; >> Config.pm:1014: my $v=(exists $Config{$_}) ? $Config{$_} : 'UNKNOW +N'; >> Config.pm:988: exists($_[0]->{$_[1]}) or >> Config.pm:882: return $_[0]->{$_[1]} if (exists $_[0]->{$_[1]}) +; >> Config.pm:885: my($value, $start, $marker, $quote_type); >> Config.pm:887: $quote_type = "'"; >> Config.pm:889: if ($_[1] eq 'byteorder') { >> Config.pm:929: $marker = "$_[1]="; >> Config.pm:932: $start = index($config_sh, "\n$marker$quote_type") +; >> Config.pm:934: if ($start == -1) { >> Config.pm:938: return undef if ( ($start == -1) && # in case it' +s first >> Config.pm:940: if ($start == -1) { >> Config.pm:947: $start += length($marker) + 2; >> Config.pm:949: $value = substr($config_sh, $start, >> Config.pm:956: if ($quote_type eq '"') { >> Config.pm:963: $value = undef if $value eq 'undef'; >> Config.pm:964: $_[0]->{$_[1]} = $value; # cache it >> Config.pm:965: return $value; >> Config.pm:1015: $v='undef' unless defined $v; >> Config.pm:1016: print "$_='$v';\n"; ccflags='-nologo -O1 -MD -DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT -DHAV +E_DES_FCRYPT -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -D PERL_MSVCRT_READFIX'; >> Config.pm:1012: foreach(@_){
        Maybe i'm wrong ...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-19 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found