Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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

by space_monk (Chaplain)
on Mar 27, 2013 at 09:58 UTC ( #1025664=note: print w/replies, xml ) Need Help??

in reply to Re^2: Curious find while comparing grep, map, and smart match...
in thread Curious find while comparing grep, map, and smart match...

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.

Replies are listed 'Best First'.
Re^4: Curious find while comparing grep, map, and smart match...
by dbuckhal (Pilgrim) on Mar 27, 2013 at 15:34 UTC

    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.

        Yes, you added an efficient random number generator, but how do you think those "shuffled" results relate to what the other results represent? What do you think I am benchmarking? How do you interpret the results you posted?

        You are pinpointing on a specific facet of my code and not looking at my code as a whole.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025664]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (14)
As of 2017-02-23 17:36 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (350 votes). Check out past polls.