Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re^2: Curious find while comparing grep, map, and smart match...

by dbuckhal (Monk)
on Mar 27, 2013 at 04:50 UTC ( #1025630=note: print w/ replies, xml ) Need Help??

Comment on Re^2: Curious find while comparing grep, map, and smart match...
Re^3: Curious find while comparing grep, map, and smart match...
by BrowserUk (Pope) on Mar 27, 2013 at 05:29 UTC

    Explain what?

    The code I posted; or why your code is inefficient?

Re^3: Curious find while comparing grep, map, and smart match...
by space_monk (Chaplain) on Mar 27, 2013 at 09:58 UTC

    I will make an effort to interpret BrowserUKs terse comment for you :-)

    In grepGen/mapGen/smartGen you have a redo in the event of a collision, whereas if you really want unique random numbers, they could be generated much more simply by the shuffle method he suggested. His method does not involve repeated retries and removes the need for any of these functions. His one liner does what each of the three methods you proposed does, but much more efficiently....

    my @rands = ( shuffle # shuffle, like a pack of cards 1 .. 120 # a series of numbers from 1..120 ) [ 0 .. 99 ]; # take the first 100 of these # the resulting @rands is a 100 element array of numbers # in the range 1..120, all of which are unique
    A Monk aims to give answers to those who have none, and to learn from those who know more.

      Thank you for your interpratation. :D I knew what he was trying to say, but in this instance he was wrong. I was measuring each of those functions against a pool of random numbers, where they would each encounter the same set, including duplicates. So, the script was written as I thought is should be to accomplish my goal.

      I always enjoy peeking around here reading the insights presented to everyone's code, even such comments which may not be directly relevant, but target the improving of one's coding skills. But, sometimes a question of what is the goal of the program can be more educational for both sides.

      As always, comments and opinions are appreciated.

        but in this instance he was wrong.

        Really? I added this sub:

        sub shuffleEm { ( shuffle 1 .. $range )[ 0 .. $uSize ]; }

        To your benchmark:

        cmpthese( -1, { grepGen => sub{ @grep = grepGen(); }, mapGen => sub{ @map = mapGen(); }, smartGen => sub{ @smart = smartGen(); }, shuffleEm=> sub{ @shuffled = shuffleEm() }, } );

        And got these times:

        C:\test>junk Rate grepGen mapGen smartGen shuffleEm grepGen 35.3/s -- -51% -97% -100% mapGen 72.6/s 106% -- -94% -100% smartGen 1250/s 3439% 1621% -- -95% shuffleEm 26859/s 75977% 36902% 2049% --

        And that doesn't include the time and memory you waste building your pointless pool of 1M values.

        But hey. It's your time you are wasting.

        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2015-04-02 08:49 GMT
Find Nodes?
    Voting Booth?

    Who makes your decisions?

    Results (63 votes), past polls