in reply to Subroutines: Returning a hash vs. hash reference
Performance is different, and return hash ref has a much better performance. Try the following test: (I tried 1000 by 200, return hash cost 20 secs, when return ref only cost 13 secs. Well... you can see how slow my PC is.)
use strict; sub return_hash { my %hash; for (0..shift) { $hash{$_} = $_; } return %hash; } sub return_hash_ref { my %hash; for (0..shift) { $hash{$_} = $_; } return \%hash; } my $ti = time(); for (0..$ARGV[1]) { my %hash = return_hash($ARGV[0]); } print "return hash takes: ", time() - $ti, " secs\n"; $ti = time(); for (0..$ARGV[1]) { my $hash = return_hash_ref($ARGV[0]); } print "return hash REF takes: ", time() - $ti, " secs\n";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Subroutines: Returning a hash vs. hash reference
by mt2k (Hermit) on Nov 28, 2002 at 04:50 UTC | |
by BrowserUk (Patriarch) on Nov 28, 2002 at 04:56 UTC |
In Section
Seekers of Perl Wisdom