From: Ned Konz Date: 2002-06-21T02:22:04+09:00 Subject: Re: profiler, which one is thread safe ? --------------Boundary-00=_ULL02IG37JUGOH3VTF4I Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable On Thursday 20 June 2002 09:32 am, Jean-Hugues ROBERT wrote: > Hello, > > I am optimizing some code and a profiler would help. > Unfortunately Ruby standard profiler seems not to work for me. > Maybe because my software is multi-threaded. I just can't get > any output at all. Try the attached, and tell me what you get. --=20 Ned Konz http://bike-nomad.com GPG key ID: BEEA7EFE --------------Boundary-00=_ULL02IG37JUGOH3VTF4I Content-Type: text/plain; charset="iso-8859-1"; name="lineprofile.rb" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="lineprofile.rb" # this uses the CPU time rather than the real time module LineProfiler__ lastNow = Start = Time.times[0] lastFile = '(toplevel)' lastLine = 0 lastTimes = [0.0] lastCounts = [0] Files = {} p = proc { |event, file, line, id, binding, klass| if event == 'line' now = Time.times[0] lastTimes[ lastLine ] += now - lastNow lastCounts[ lastLine ] += 1 if lastFile != file (lastTimes, lastCounts) = Files.fetch(file) { |name| Files[name] = [ Array.new(10000, 0.0), Array.new(10000, 0) ] } lastFile = file end lastLine = line lastNow = Time.times[0] end } END { set_trace_func nil total = Time.times[0] - Start realTotal = 0.0 for file in Files.keys next if file == __FILE__ (times, counts) = Files[file] times.each { |t| realTotal += t } end printf("Profiling took %d%% of total time\n", (total-realTotal)/total) realTotal /= 100.0 # convert to percent f = STDERR for file in Files.keys.sort next if file == __FILE__ f.printf("=========== %s\n", file) f.printf(" Line time%% time count\n", file) lines = IO.readlines(file) (times, counts) = Files[file] lines.each_index do |line| if counts[line+1] > 0 f.printf("%5d %3.0f%% %6.2f %6d %s", line+1, times[line+1]/realTotal, times[line+1], counts[line+1], lines[line]) else f.printf("%5d %s", line+1, lines[line]) end end end f.close } set_trace_func p end --------------Boundary-00=_ULL02IG37JUGOH3VTF4I--