Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: When the Best Solution Isn't

by Anonymous Monk
on Apr 01, 2005 at 09:29 UTC ( #444125=note: print w/replies, xml ) Need Help??


in reply to Re: When the Best Solution Isn't
in thread When the Best Solution Isn't

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.

Replies are listed 'Best First'.
Re^3: When the Best Solution Isn't
by BrowserUk (Pope) on Apr 01, 2005 at 10:28 UTC
    Mindboggling. "qshuf", an N log N algorithm, ...

    Hmm. N log N with N=4 == 2.408. How does that affect your boogle? The main part of the post was the statistical analysis, but you are correct, the void context does make a difference to the timing and the output posted was obviously an accumulation from 2 different runs of the program which had been modified in the interim.

    Hmmm, with a first argument of '10' to 'cmpthese'? Either your output doesn't match your program

    It was a long time ago, so I am guessing more than remembering, but I think what probably happened was that I wrote the benchmark code first, ran it (with the iters counter set to 10000) and c&p'd the output to the source file.

    Seeing that qshuf() seemed very fast, (I probably wasn't even aware that sort silently optimised for void context rather than doing something sensible like issuing the "Useless in a void context" warning as other parts of perl do). I then went on to add the statistical analysis, and dropped the iterations counter to 10 to save time while getting the formatting of the output right.

    Here is a an updated version (run on 5.6.1 as the original was):

    And by way of recompense, here's an updated version incorporating List::Util::shuffle run on 5.8

    Thanks for pointing out the discrepancies--even if it did take 2 years for collected populace to notice :)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
      Hmm. N log N with N=4 == 2.408. How does that affect your boogle?
      Not at all. Three points:
      1. In CS, when people talk about log, they mean 2log, and 4 2log 4 == 8.
      2. I don't know what significance 4 has. The array has a 1000 elements, so N equals 1000, so N log N would be about 9966.
      3. But numbers don't mean anything. When we talk about a linear, or a quadratic, or a N log N, or some other function of N algorithm, we talk about how it scales. Any constants involved are ignored.

        You didn't notice the smiley?

        I conceded, and thanked you for, all your points. And posted updated code and stats.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2019-11-15 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (83 votes). Check out past polls.

    Notices?