Hi,

Here is a small test that shows the problem (memory consumption of counting the 
size of a huge hash, with not-so-small keys)

-------
my %hash;
for (my $i=0; $i<5000000;$i++){
        $hash{$i x 100}=$i};
print "OK\n";
sleep 20;  ###  Here the memory is about 3G
print (scalar keys %hash,"\n");
 sleep 10  ### here the memory is 3.5 G
------

Well, it means the list is being built, and it does consume a lot of memory 
(and CPU- but this is not my problem here).  In my real application it crashes, 
since I have bigger hash/keys, and I am getting "out of memory" crash.

Isn't there a better way to get the hash size ?

Yossi

-----Original Message-----
From: Perl [mailto:[email protected]] On Behalf Of Gabor Szabo
Sent: Wednesday, May 21, 2014 11:00 PM
To: Perl in Israel
Subject: Re: [Israel.pm] Perl performance optimization question

On Wed, May 21, 2014 at 3:00 PM, Yossi Itzkovich 
<[email protected]<mailto:[email protected]>> wrote:
> Hi,
>
>
>
> Suppose I have a huge hash. when I write
>
> my $n= keys %hash;
>
>
>
> Will it consume time and RAM for building the unneeded list (before
> taking the size from it), or will it return the size immediately ?
>
> If it does cost me a lot, can I optimize it ?
>

The documentation does not say anything about this, and I am not sure, but I 
don't see a reason why in scalar context 'keys' would need to create an array 
in memory.

In any case, I wonder why are you concerned?
Have you encountered an issue with an application?
If not, then just use it and if you encounter a problem then check if the 
source of the problem is this statement.

Or write a script that does this with a huge hash and check the memory 
footprint.
This could be a nice article.

Or, if you really want to be sure, check the source code of perl.

regards
   Gabor
_______________________________________________
Perl mailing list
[email protected]<mailto:[email protected]>
http://mail.perl.org.il/mailman/listinfo/perl

_______________________________________________
Perl mailing list
[email protected]
http://mail.perl.org.il/mailman/listinfo/perl

Reply via email to