From: Ola Bini Date: 2006-05-25T22:39:15+09:00 Subject: Re: How use Profiling, a Quick Guide. ----- Original Message ----- From: Pedro C�rte-Real Date: Thursday, May 25, 2006 12:41 pm Subject: Re: How use Profiling, a Quick Guide. To: ruby-talk@ruby-lang.org (ruby-talk ML) > 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. > > Pedro. > > Hi, this is interesting, and I tried something like it. It works fairly well, but DON'T try to trace :+ with this, for obvious reasons... =) /O