From: Victor Shepelev Date: 2006-05-26T00:37:48+09:00 Subject: Re: How use Profiling, a Quick Guide. From: Robert Klemme [mailto:shortcutter@googlemail.com] Sent: Thursday, May 25, 2006 6:33 PM > 2006/5/25, Pedro C�rte-Real : > > On 5/24/06, Victor Shepelev wrote: > > > Here can help my Module#add_tracer, described here > > > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192291 > > > > That's a nice idea. I made a small modification to count the totals of > > each of the calling methods instead of printing each time: > > > > $traces = Hash.new(0) > > > > class Module > > def add_tracer(meth) > > m_alias = case meth > > when :[] : "old_idx" > > when :+ : "old_plus" > > when :- : "old_minus" > > #and so on > > else ; "old_#{meth}" > > end > > module_eval %Q{ > > alias :#{m_alias} :#{meth} > > def #{meth}(*arg, &block) > > str = caller[0]+"-"+self.class.name+"-##{meth}" > > $traces[str] += 1 > > #{m_alias}(*arg, &block) > > end > > } > > end > > end > > > > Array.add_tracer(:each) > > > > .... > > Your code here > > .... > > > > $traces.each {|name, value| puts "#{name} - #{value} calls"} > > > > You can also sort it by the value in the hash to get an ordered list. > > This will do for a hack but writing a tracing gem would be great. > > > Note that a similar thing can be achieved with set_trace_func Drawbacks of set_trace_func was discussed in the original topic here: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/192294 Victor.