You wrote:
> Now I understand that 'keys' would have to
> generate a list which might be expensive I
> guess, but I'm still a bit puzzled by the results.
Well, you hit the proverbial nail on the head there,
though. So I'm not sure there's much to be puzzled
about, unless you're just wondering why keys is
so slow at generating the list.
But it is that very fact--that keys has to generate a
list of the keys each time--that makes it so much
slower when breaking early out of the loop. Simply
put, you need to do *much* less work when you
break early out of the loop when using each, because
you haven't had to iterate over the entire hash and
build a list.
If, for example, you build the list of keys before you
run the benchmark, then in k iterate over
that list instead of calling keys anew, you'll find
that indeed it is that call that's slowing it down.
-
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.
|