Steve, Great explanation! Thanks!
-- Josh http://iammrjoshua.com Steve Ross wrote: > On Feb 4, 2009, at 9:14 AM, Joshua Abbott wrote: > >> Have you done any benchmark testing on using the &: method? I would be >> curious as to why you say it's slower. > > PMFJI... As I recall, the &:foo notation is a shortcut for > Symbol#to_proc so the following code snippets are the same: > > def frazzle > self.name.split(//).sort_by{rand}.join('') > end > > @stuff.each(&:frazzle) > > @stuff.each{|s| Proc.new{|*args| args.shift.__send__(s, *args)}} > > Look in active_support/symbol.rb > > So what could make this slower are the facts that 1) you are creating > a new anonymous proc for each iteration through the @stuff collection; > and 2) you are using the send method to invoke the method referenced > by the symbol. Contrast that to: > > @stuff.each{|s| s.frazzle} > > In this case, the method is being invoked directly, skipping the steps > of creating the anonymous proc and then invoking "frazzle" via a send. > > So, that's how I understand it. Now, in the case where you have a > dozen users returned and want to list their names next to checkboxes, > the performance difference is negligible. It's the case where you > can't predict the size of the result set that you want to beware of. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---