Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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...


In reply to Re^2: Using foreach to process a hash by blazar
in thread Using foreach to process a hash by iridius

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (None)
    As of 2024-04-19 00:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found