-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |