From: Robert Klemme Date: 2006-05-26T00:33:26+09:00 Subject: Re: How use Profiling, a Quick Guide. 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 $ ruby -e 'set_trace_func lambda {|*a| p a}; "".length' ["line", "-e", 1, nil, #, false] ["c-call", "-e", 1, :length, #, String] ["c-return", "-e", 1, :length, #, String] You just need to evaluate parameters appropriately. See also http://ruby-doc.org/docs/ProgrammingRuby/html/ospace.html Kind regards robert -- Have a look: http://www.flickr.com/photos/fussel-foto/