Another question about memory efficiency 
Author Message
 Another question about memory efficiency

Earlier this week I asked about the huge amount of memory my simple
program managed to hog, and I got some good advice to replace


  # blah

Quote:
}

with:

my $line;

  # blah

Quote:
}

and that made a lot of difference. But not enough. Because I also have
the following unhealthy construction:

foreach $key (keys ($huge_hash_with_many_keys)) {
  # Blah

Quote:
}

What would be the best way to deal with this one to improve memory
efficiency?

Regards,
    Frank

============================================

Homepage: http://www.*-*-*.com/ ~frankvw
ICQ #: 13800170
============================================



Wed, 18 Jun 1902 08:00:00 GMT  
 Another question about memory efficiency
On Wed, 05 Apr 2000 22:16:51 +0200, Frank van Wensveen

Quote:

> foreach $key (keys ($huge_hash_with_many_keys)) {
>   # Blah
> }

Ehm... that should be, of course,

Quote:
> foreach $key (keys (%huge_hash_with_many_keys)) {
>   # Blah
> }

Regards,
    Frank

============================================

Homepage: http://www.euronet.nl/~frankvw
ICQ #: 13800170
============================================



Wed, 18 Jun 1902 08:00:00 GMT  
 Another question about memory efficiency


Quote:
> Earlier this week I asked about the huge amount of memory my simple
> program managed to hog, and I got some good advice to replace


>   # blah
> }

> with:

> my $line;

>   # blah
> }

You might want to localize $line directly in the operand of the 'while'
loop.


Quote:
> and that made a lot of difference.

It's not clear why that made a lot of difference.  I learned here not
too long ago that in the 'foreach' case, the array is not copied again
into a list to be iterated over.  The elements are simply aliased in
turn.  Perhaps you refer to a situation where a file is being read one
line at a time instead of into a huge array of many lines, so the huge
array never exists.

Quote:
>                                     But not enough. Because I also have
> the following unhealthy construction:

> foreach $key (keys ($huge_hash_with_many_keys)) {

As corrected in your followup post.  :-)

Quote:
>   # Blah
> }

> What would be the best way to deal with this one to improve memory
> efficiency?

Quite analogous to the array situation.

    while (defined(my $key = each %huge_hash_with_many_keys)) {

You can also retrieve the value directly in the same hash access.

perldoc -f each

Quote:
> Regards,
>     Frank

Please insert a string like "\n-- \n" to help newsreaders cut your
signature, so responders don't have to do it manually.

...

--
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/



Wed, 18 Jun 1902 08:00:00 GMT  
 Another question about memory efficiency

Quote:

> I also have the following unhealthy construction:

> foreach $key (keys ($huge_hash_with_many_keys)) {
>   # Blah
> }

> What would be the best way to deal with this one to improve memory
> efficiency?

You could parallel what you did with the array:


my $key;

   # Do stuff

Quote:
}

It's important to note that this approach doesn't reduce your memory
usage per se. You still have to allocate space to hold all the hash
keys. It just gradually frees up that space by shifting them off the
array. This frees up that space to be used by other parts of the program
without needing to claim more system resources.

Using each() may work better for you, because it returns individual
key/value pairs:

my ($key, $value);
while (($key, $value) = each %huge_hash_with_many_keys) {
    # Do stuff

Quote:
}

Or, if you want only the keys w/o the values, you can call each() in a
scalar context:

while (defined($k = each %huge_hash_with_many_keys)) {
    # Do stuff

Quote:
}

The defined() is needed in case you have "0" or "" for keys. There are
some things to be careful of, see 'perldoc -f each' for more.

-mjc



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. memory efficiency question

2. perl memory efficiency

3. Efficiency question

4. Question about efficiency

5. Perl5 objects & efficiency questions

6. Perl efficiency question

7. some questions about array refs & efficiency

8. Quick Question on Efficiency

9. run-time eval efficiency question

10. Time-efficiency question

11. Bizarre efficiency question.

12. Rookie question on efficiency of split/join vs loop

 

 
Powered by phpBB® Forum Software