in reply to performance of counting keys in a big hash
The documentation of keys says: "In scalar context, returns the number of keys or indices." Based on this I would say that keys has a shortcut to return the number of keys w/o building an array first. A little experiment seems to confirm this:
use strict; use warnings; use Time::HiRes qw/time/; my %hash; my $n = 1000000; @hash{1..$n} = 1 x $n; my $s = time(); my @k = keys %hash; print scalar @k, ": "; print time()-$s, "\n"; $s = time(); print scalar keys %hash, ": "; print time()-$s, "\n";
The second version is vastly faster.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: performance of counting keys in a big hash
by space_monk (Chaplain) on May 22, 2013 at 11:54 UTC | |
by hdb (Monsignor) on May 22, 2013 at 12:13 UTC | |
by space_monk (Chaplain) on May 22, 2013 at 12:19 UTC | |
Re^2: performance of counting keys in a big hash
by demerphq (Chancellor) on May 22, 2013 at 13:16 UTC | |
by hdb (Monsignor) on May 22, 2013 at 13:23 UTC | |
by demerphq (Chancellor) on May 22, 2013 at 13:44 UTC | |
by space_monk (Chaplain) on May 22, 2013 at 14:55 UTC |