[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: sorting by rand?

seebs

4/30/2007 7:47:00 AM

In message <4635AB25.9050809@gmail.com>, Dan Zwell writes:
>You're right. I guess I'm just inclined to discount a such a small
>difference in orderings. And the difference in our benchmarks may have
>been due to the fact that I'm using a different algorithm (for each
>element i, swap i with an element from i...end).

Hmm. I don't *think* that makes a difference, although I'm not sure.
It may affect the results.

>Or perhaps because I
>have more method calls, as I made a method Array#randomize! (that calls
>Array#swap! n times)

That might well do it; method lookup is comparatively expensive, I
think.

... Of course, the entire thing is probably silly, in that I doubt most
programs spend much time unsorting arrays. I was mostly just curious
about the question of what happens if you try to provide an un-fixed
sort key. For sort_by, they're cached; sort { rand <=> rand } might
be unpredictable, though.

-s

1 Answer

Dan Zwell

4/30/2007 8:02:00 AM

0

Peter Seebach wrote:
>> Or perhaps because I
>> have more method calls, as I made a method Array#randomize! (that calls
>> Array#swap! n times)
>
> That might well do it; method lookup is comparatively expensive, I
> think.
>
> ... Of course, the entire thing is probably silly, in that I doubt most
> programs spend much time unsorting arrays. I was mostly just curious
> about the question of what happens if you try to provide an un-fixed
> sort key. For sort_by, they're cached; sort { rand <=> rand } might
> be unpredictable, though.
>

That did it--I replaced calls to swap!() with three lines of code, and
now my algorithm is faster on arrays with as little as 40 elements!

Dan