If you accept the principle that code in modules is destined to be reused in lots of situations, including performance critical ones, and should therefore do it's utmost to be as efficient as the author is comfortable maintaining, then List::Util's use of aliasing to sqeeze a little more performance seems entirely reasonable.
I'm not saying it isn't. (Well, except that some comments for the benefit of the occasional peeping Tom would have been nice.) In fact I'm amazed, because I wouldn't have expected it to be that efficient. The question still is: how does one come up with such code? I suspect that one critical difference is that the naive implementation, due to its de facto shuffling of an array in place, does so by taking two slices, and assigning to one of them. While L::U's doesn't really swap, and the overhead of dereferencing weights less. BTW, testing with
for my $i (map 10**$_, 1..4) {
cmpthese -30, { map { $_ => "$_ 1..$i" } qw/naive buk/ };
}
one seems to get a slowly decreasing trend:
Rate naive buk
naive 49811/s -- -37%
buk 79547/s 60% --
Rate naive buk
naive 4751/s -- -33%
buk 7112/s 50% --
Rate naive buk
naive 479/s -- -29%
buk 677/s 41% --
Rate naive buk
naive 46.9/s -- -27%
buk 64.6/s 38% --
That said, they missed a trick. Using a block form map creates a additional level of scope that they aren't using and slows performance.
They can squeeze another 30%, and almost 50% over the 'naive' implementation, by avoiding it
Cool!
-
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.