sub qshuf {
sort { .5 <=> rand(1) } @_;
}
cmpthese(10, {
slice => sub { slice @array },
xform => sub { xform @array },
shufl => sub { shufl @array },
qshuf => sub { qshuf @array },
});
Rate xform slice shufl qshuf
xform 14.4/s -- -38% -70% -100%
slice 23.3/s 61% -- -51% -99%
shufl 47.8/s 232% 105% -- -99%
qshuf 3284/s 22681% 14008% 6767% --
Mindboggling. "qshuf", an N log N algorithm, shows to be 67 faster than any of linear algorithms, and you don't raise a flag?
Your benchmark is extremely flawed. You're running your sort in void context. Perl, being smart, knows that sort in void context isn't going to do anything useful, so it's not going to work up a sweat, and just won't do anything.
Benchmark: timing 10000 iterations of qshuf, shufl, slice, xform
Hmmm, with a first argument of '10' to 'cmpthese'? Either your output doesn't match your program, or your Benchmark.pm module is different than mine.
-
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.
|