From: Robert Klemme Date: 2004-12-01T17:47:45+09:00 Subject: Re: profile "Brian Schr�der" schrieb im Newsbeitrag news:20041130230416.6299104d@black.wg... > On Wed, 1 Dec 2004 06:52:47 +0900 > "Joe Van Dyk" wrote: > > > Hi, > > > > Still pretty new to Ruby and profiling. Are there any tools I can use to > > analyze my Ruby program to see where it's spending the bulk of the CPU time? > > > > Thanks, > > Joe > > > > > > ruby -rprofile -e'puts (1..200).inject(0) { | r, i | r + (-1)**i * 1.0 / i }' > -0.690653430481824 > % cumulative self self total > time seconds seconds calls ms/call ms/call name > 44.44 0.04 0.04 1 40.00 80.00 Range#each > 22.22 0.06 0.02 200 0.10 0.10 Fixnum#** > 22.22 0.08 0.02 200 0.10 0.10 Float#/ > 0.00 0.08 0.00 1 0.00 0.00 Fixnum#+ > 0.00 0.08 0.00 200 0.00 0.00 Fixnum#* > 0.00 0.08 0.00 1 0.00 0.00 Kernel.puts > 0.00 0.08 0.00 2 0.00 0.00 IO#write > 0.00 0.08 0.00 199 0.00 0.00 Float#+ > 0.00 0.08 0.00 1 0.00 80.00 Enumerable.inject > 0.00 0.08 0.00 1 0.00 90.00 #toplevel > 0.00 0.08 0.00 1 0.00 0.00 Profiler__.start_profile > 0.00 0.08 0.00 1 0.00 0.00 Float#to_s > And an example for benchmark usage: require 'benchmark' include Benchmark N = 10000 sample = ("foulx" * 100).freeze bm(20) do |b| b.report("gsub!") do N.times do s = sample.dup s.gsub!(/x/, 'u') end end b.report("gsub") do N.times do s = sample.dup s = s.gsub(/x/, 'u') end end end 09:41:57 [robert.klemme]: ruby /c/temp/ruby/stringperf.rb user system total real gsub! 3.203000 0.000000 3.203000 ( 3.242000) gsub 3.203000 0.000000 3.203000 ( 3.250000) Regards robert