Skip to content

very simple Ruby line (sample) profiler

License

Notifications You must be signed in to change notification settings

Nakilon/lineprofiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gem lineprofiler

Usage

gem install lineprofiler
ruby -r lineprofiler my_slow_program.rb

On exit (wether the program exited or you've interrupted it via ^C) it will print the line profiling report, like this:

...
88.97%  loop do
84.14%    combinations.each do |f, set|
48.97%      t = all.size.times.map do |i|
              [
                all[i].cls,
43.92%          all.values_at(*set-[i]).min_by{ |e| Math.s
              ]
            end
          end
 1.97%    combinations = pcbr.table.sort_by{ |_, _, score|
 1.97%      set.size.times.map do |i|
 1.31%        next if set.size < 3
 0.65%        next if pcbr.set.include? [f, key]
              [f, key]
            end
 2.60%    end.drop_while(&:empty?).first
...

or like this:

...
94.58%  items.group_by{ |x,y,| (px-x).abs + (py-y).abs }...
 0.49%    puts "dist: #{d}"
          add = g.flat_map(&:last).sort
          until add.empty?
            acc.replace acc | add
93.63%      a, b = all.partition do |id, rec|
55.88%        unless (rec & add).empty?
24.51%          if (rec - acc).empty?
13.24%            (puts "#{id} = #{rec.join " + "}"; true)
                end
...

By default it reports only about the main file -- $0, but you can pass a path to another source file (for example a library dependency that your program calls often) as a LINEPROFILER env var so it will print it too afterwards:

$ LINEPROFILER=lib/rasel.rb ruby -rlineprofiler constants_print.rb
...
constants_print.rb
  ...  
lib/rasel.rb
  ...

You are supposed to pass this env var if you run the code via -e. Also note that in the next example we require the library we want to profiler before the profiler so we don't profile the initialization but only the invocation step:

$ LINEPROFILER=common.rb bundle exec ruby -r./common -rlineprofiler -e 'puts Common.my_method'