On Oct 8, 2007, at 10:05 , Rick DeNatale wrote: > On 10/8/07, Eric Hodel <drbrain / segment7.net> wrote: >> Profiling indicated a significant amount of time was spent in #<=> >> which created more objects and called more methods than #sort_by. >> Replacing #sort with #sort_by and #sort_obj reduced both. Creating >> fewer objects and using less memory aren't the same thing. >> >> (Also, Rick's benchmark shows Array#sort cheating more than any other >> effect.) > > First of all it's not my benchmark, it comes directly from the ri doc > for sort_by > > Second, I don't understand the concept of Array#sort "cheating" this > isn't a contest. Array#sort doesn't call Fixnum#<=> when comparing fixnums, even if you redefine Fixnum#<=>. If you want a benchmark that shows when #sort is faster than #sort_by in several general cases you should use like implementations, not the special cases where Array#sort has been optimized. > Some things perform better with sort, others with sort_by. The > performance of sort is generally determined by the cost of doing the > <=> comparison, while the performace of sort_by is generally > determined by the cost in time and space of creating the side array. For sorting Fixnum there is much lower cost since you never call a #<=>. While I didn't mention it, I was profiling to speed up RubyGems. I don't blindly assume, and neither should anyone else. -- Poor workers blame their tools. Good workers build better tools. The best workers get their tools to do the work for them. -- Syndicate Wars