Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Using foreach to process a hash

by blazar (Canon)
on Oct 22, 2006 at 13:30 UTC ( [id://579856]=note: print w/replies, xml ) Need Help??


in reply to Re: Using foreach to process a hash
in thread Using foreach to process a hash

Concerning your question, jdporter answered it but I felt obligated to contribute. If you're processing a hash of considerable size, obviously you'd prefer to use the most efficient functions possible. In order to accomplish this end, the standard perl library includes a module called Benchmark (please see below for an example):

I beg to differ. I've done similar interventions before and I know the subject is controversial, but I don't mind being downvoted. Don't misunderstand me: Benchmark.pm is great and I use it quite often, i.e. whenever I really need it. Indeed had the OP called for 'efficiency', it may have been a prefectly sensible answer. But whenever one calls for 'efficiency' one bell should ring, and often does: the very question is whether efficiency would be relevant at all in the situation under consideration. Sometimes it is, sometimes it's not. Actually in the latter case it often turns out to be yet another case of obsession for premature optimization which, we all know, is the root all evil in programming.

Now the point is, I see a risk in pointing a newbie like iridius towards these issues: precisely the risk of generating or contributing to that obsession for premature optimization...

OTOH your code has a bug:

C:\temp>dk Benchmark: timing 1000000 iterations of foreach_loop, while_each_loop. +.. foreach_loop: 0 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ +927643.78/ s (n=1000000) while_each_loop: 0 wallclock secs ( 0.50 usr + 0.00 sys = 0.50 CPU) + @ 2000000 .00/s (n=1000000) C:\temp>perl -w dk.pl Name "main::hash" used only once: possible typo at dk.pl line 17. Benchmark: timing 1000000 iterations of foreach_loop, while_each_loop. +.. foreach_loop: 2 wallclock secs ( 1.08 usr + 0.00 sys = 1.08 CPU) @ +927643.78/ s (n=1000000) while_each_loop: 1 wallclock secs ( 0.52 usr + 0.00 sys = 0.52 CPU) + @ 1941747 .57/s (n=1000000) C:\temp>perl -wMstrict dk.pl Global symbol "%hash" requires explicit package name at dk.pl line 17. Execution of dk.pl aborted due to compilation errors.

Indeed you forgot to change $hash{$key} to $monks{$key} in code lifted from previous example, I suppose. In this case it doesn't alter excessively the results, but in others, a similar error may do, and in relevant manners.

So I felt obligated to contribute too, but to the effect of reminding the OP that as the above clearly shows, just inserting

use strict; use warnings;

at the top of quite about all of his scripts is probably the best way to avoid many common programming mistakes. It just tells perl to give one all the help it can to avoid them.

One last piece of advice I can give him, also inspired by this example, is to use descriptive names for his variables: e.g. $monk instead of $key and $rank instead of $value. The rationale being that if it doesn't make sense when mentally translated into English, then chances are it may be wrong...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://579856]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (8)
As of 2024-04-24 10:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found