From: Lionel Bouton Date: 2007-08-27T03:31:23+09:00 Subject: Re: sort_by { rand } not working James Edward Gray II wrote: > > It would be better to use: > > data = data.sort_by { … } I was wondering about a crazy idea of mine: "sort!{rand<=>rand}" and from my bench results it's a waste of time (it's even dangerous, see below). I suspect sort_by begins by mapping the block results and then sorts based on the map (it's roughly 4x faster than sort!{ rand <=> rand }). sort!{rand<=>rand} obviously can't do that and must call the block each time a comparison must be done (I suspect that more rand calls make it slower even if it can avoid copying things and instead do in-place modifications ... or that the Ruby sort algorithm doesn't like comparison results changing...). Obviously depending on the sort algorithm used it might not even converge on a result (the dangerous part...). Lionel