use strict; use warnings; use Benchmark; use Data::Dumper; # Note: You can increment a string in Perl! WOW! # As long as you don't use the string in a numeric context! # $string++ is quite different than $string +=1 # # Below, this is used to make a bunch of unique hash keys # I don't think that the fact that they are sequential in # an alphabetic sense makes much difference in the generated # hash table because of the way that the Perl hash algorithm # works. sub return_hash { # create a large hash and return that entire hash as list my $string = "ABCDEFGHIJ"; my %hash = map{$string++ => 1}(1..100000); return %hash; } sub return_hash_ref { # create a large hash and return a ref to that hash my $string = "ABCDEFGHIJ"; my %hash = map{$string++ => 1}(1..100000); return \%hash; } sub return_just_keys { # create a large hash and return just the keys of that hash my $string = "ABCDEFGHIJ"; my %hash = map{$string++ => 1}(1..100000); return keys %hash; } timethese(1000, { '1)Keys of Hash via list' => 'my @keys = keys %{{return_hash()}}', '2)Keys of Local Hash copy' => 'my %hash2 = return_hash(); my @keys = keys %hash2;', '3)Keys of local Hash Ref' => 'my $href = return_hash_ref(); my @keys = keys %$href;', '4)Just the returned keys' => 'my @keys = return_just_keys()', }); __END__ Benchmark: timing 1000 iterations of 1)Keys of Hash via list, 2)Keys of Local Hash copy, 3)Keys of local Hash Ref, 4)Just the returned keys... 1)Keys of Hash via list: 182 wallclock secs (179.20 usr + 1.97 sys = 181.17 CPU) @ 5.52/s (n=1000) 2)Keys of Local Hash copy: 184 wallclock secs (182.69 usr + 0.42 sys = 183.11 CPU) @ 5.46/s (n=1000) 3)Keys of local Hash Ref: 130 wallclock secs (129.56 usr + 0.72 sys = 130.28 CPU) @ 7.68/s (n=1000) 4)Just the returned keys: 125 wallclock secs (125.20 usr + 0.52 sys = 125.72 CPU) @ 7.95/s (n=1000)