Skip to content

Commit

Permalink
the first version and README
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakilon committed Feb 28, 2021
1 parent 2de1c66 commit a4477d5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 1,35 @@
# Ruby line profiler

I'll publish it as a gem. Maybe. Later.

## Usage

```bash
ruby -r lineprofiler my_slow_program.rb
```

On exit it will print something like this:

```none
...
best = nil
prev_time = nil
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
break if Time.now - prev_time > 10
...
```
16 changes: 16 additions & 0 deletions lib/lineprofiler.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,16 @@
profile = []
Thread.abort_on_exception = true
Thread.new do
loop do
profile.push Thread.main.backtrace_locations.map{ |loc| [loc.absolute_path, loc.lineno] }
sleep 0.01
end
end
END {
File.foreach($0).with_index do |line, i|
v = profile.count do |bt|
bt.include? [File.expand_path($0), i 1]
end.fdiv(profile.size).round(4) * 100
puts "%-8s%s" % [("%5.2f%%" % v unless v.zero?).to_s, line[0,50]]
end
}

0 comments on commit a4477d5

Please sign in to comment.