From: Stefan Rusterholz Date: 2007-08-27T07:26:59+09:00 Subject: Re: sort_by { rand } not working Lionel Bouton wrote: > Dan Zwell wrote: >> >> for i in 0...@all.length >> j = i+rand(@all.length-i) >> @all[i], @all[j] = @all[j], @all[i] >> end >> >> The second one will be faster, but you probably shouldn't care on >> small arrays. >> > > You could even optimize this further with 0...@all.length-1 instead of > 0...@all.length (the last loop is a noop), but I agree, this is too much > pain for the eyes to bother unless it becomes a real performance > problem. You could even use @all.length.times :) (IMHO more readable, speed wise it is faster for small arrays, slower for big arrays, break even is here around 100 items). Regards Stefan -- Posted via http://www.ruby-forum.com/.