Perl takes a long time to evaluate:% hash / iterate keys through a large hash

In a Perl script, I create a large hash (about 10 GB) that takes about 40 minutes, which contains about 100 million keys. Then I want to skip the hash keys, for example:

foreach my $key (keys %hash) {

However, this line takes 1 hour and 20 minutes! Once in the for-loop, the code goes through the entire hash at a fast pace.

Why does logging in forloop take so long? And how can I speed up the process?

+4
source share
2 answers
foreach my $key (keys %hash) {

, %hash, , %hash , , . , .

while (my ($key, $value) = each %hash) { , . , , .

+8

, .

1:

foreach my $k (keys %h)
{
  print "key: $k, value: $h{$k}\n";
}

:

  • .

:

  • , , , .

2:

while ( ($k, $v) = each %h )
{
  print "key: $k, value: $h{$k}\n";
}

:

  • , , each , (, ).

:

  • .
  • , , %h. , keys %h, values %h each %h, , %h 1
+6

Source: https://habr.com/ru/post/1534861/


All Articles