V
viki
sub RandomElement { # select random element from the hash
my ($hashref) = @_;
my @keys = keys(%{$hashref});
my $random = $hashref->{ $keys[ int(rand( @keys )) ]};
}
When the hash is large, the creation of @keys for every
invocation is invefficient. It's waste that's O((N)) for every call to
RandomElement.
What would be the fastest version of RandomElement ?
Thanks
Viki
my ($hashref) = @_;
my @keys = keys(%{$hashref});
my $random = $hashref->{ $keys[ int(rand( @keys )) ]};
}
When the hash is large, the creation of @keys for every
invocation is invefficient. It's waste that's O((N)) for every call to
RandomElement.
What would be the fastest version of RandomElement ?
Thanks
Viki