Skip to content

Instantly share code, notes, and snippets.

@shawn42
Created November 29, 2011 06:48
Show Gist options
  • Save shawn42/1403753 to your computer and use it in GitHub Desktop.
Save shawn42/1403753 to your computer and use it in GitHub Desktop.
Performance investigation
puts "\nGARBAGE COLLECTION"
# Not even close to exact, but gives a rough idea of what's being collected
old_objects = ObjectSpace.count_objects.dup
ObjectSpace.garbage_collect
new_objects = ObjectSpace.count_objects
old_objects.each do |k,v|
diff = v - new_objects[k]
puts "#{k} #{diff} diff" if diff != 0
end
GARBAGE COLLECTION
FREE -22353 diff
T_OBJECT 6 diff
T_FLOAT 10806 diff
T_STRING 521 diff
T_ARRAY 9263 diff <-- a lot of arrays being collected?
T_HASH 312 diff
T_STRUCT 862 diff
T_DATA 563 diff
T_NODE 20 diff
shawn42@bits ~/code/games/princess_game (git::master)✗ $ be pprof.rb --text /tmp/princess_perf.txt
Using local file /Users/shawn42/.rvm/rubies/ruby-1.9.3-p0/bin/ruby.
Using local file /tmp/princess_perf.txt.
Total: 313 samples
106 33.9% 33.9% 266 85.0% Gosu::Window#show_internal
47 15.0% 48.9% 47 15.0% garbage_collector
25 8.0% 56.9% 40 12.8% Range#each
19 6.1% 62.9% 42 13.4% GraphicalActorView#draw
7 2.2% 65.2% 8 2.6% Hash#[]=
6 1.9% 67.1% 10 3.2% Arbiter#collide_circle_circle?
shawn42@bits ~/code/games/princess_game (git::master)✗ $ be pprof.rb --text /tmp/princess_perf.txt
Using local file /Users/shawn42/.rvm/rubies/ruby-1.9.3-p0/bin/ruby.
Using local file /tmp/princess_perf.txt.
Total: 225 samples
80 35.6% 35.6% 80 35.6% garbage_collector
48 21.3% 56.9% 145 64.4% Gosu::Window#show_internal
9 4.0% 60.9% 10 4.4% Hash#[]=
7 3.1% 64.0% 24 10.7% GraphicalActorView#draw
6 2.7% 66.7% 11 4.9% Arbiter#collide_circle_circle?
require 'perftools'
PerfTools::CpuProfiler.start("/tmp/princess_perf.txt")
# let code execute here
# ...
PerfTools::CpuProfiler.stop
c-call /Users/shawn42/.rvm/gems/ruby-1.9.3-p0/gems/gamebox-0.3.3/lib/gamebox/spatial_hash.rb:139 initialize Array
c-return /Users/shawn42/.rvm/gems/ruby-1.9.3-p0/gems/gamebox-0.3.3/lib/gamebox/spatial_hash.rb:139 initialize Array
c-call /Users/shawn42/.rvm/gems/ruby-1.9.3-p0/gems/gamebox-0.3.3/lib/gamebox/spatial_hash.rb:139 initialize Array
c-return /Users/shawn42/.rvm/gems/ruby-1.9.3-p0/gems/gamebox-0.3.3/lib/gamebox/spatial_hash.rb:139 initialize Array
c-call /Users/shawn42/.rvm/gems/ruby-1.9.3-p0/gems/gamebox-0.3.3/lib/gamebox/spatial_hash.rb:139 initialize Array
def toggle_trace
@do_trace = !@do_trace
if @do_trace
set_trace_func proc { |event, file, line, id, binding, classname|
printf("%8s %s:%-2d %10s %8s\n", event, file, line, id, classname) if classname == Array && id == :initialize
}
else
set_trace_func nil
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment