From: Victor Shepelev Date: 2006-05-25T22:30:04+09:00 Subject: Re: How use Profiling, a Quick Guide. From: Pedro Corte-Real [mailto:pedrocr@gmail.com] Sent: Thursday, May 25, 2006 1:41 PM > 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. Yep, it's a very good point! BTW, it can have sense to gather all those "profiling hacks" (like this #add_tracer, and John's NewProfile and memprofile) to some rubyforge project? > Pedro.