Skip to content

Instantly share code, notes, and snippets.

@agrimm
Created June 12, 2014 23:23
Show Gist options
  • Save agrimm/a69f7f0f5e238ae2a65b to your computer and use it in GitHub Desktop.
Save agrimm/a69f7f0f5e238ae2a65b to your computer and use it in GitHub Desktop.
srand 0
WIDTH, HEIGHT = 10, 10
array = Array.new(HEIGHT){ [0]*WIDTH }
require "profile"
10000.times do
y, x = rand(HEIGHT), rand(WIDTH)
g = array[y][x] + [-1,+1].sample
array[y][x] = g unless [[y-1,x],[y+1,x],[y,x-1],[y,x+1]].any?{ |y, x|
y>=0 && y<HEIGHT && x>=0 && x<WIDTH && 1 < (array[y][x] - g).abs
}
end
% cumulative self self total
time seconds seconds calls ms/call ms/call name
44.00 0.44 0.44 38175 0.01 0.03 nil#
17.00 0.61 0.17 10000 0.02 0.04 Array#each
12.00 0.73 0.12 20000 0.01 0.01 Kernel#rand
11.00 0.84 0.11 10000 0.01 0.05 Enumerable#any?
6.00 0.90 0.06 1 60.00 1000.00 Integer#times
4.00 0.94 0.04 25500 0.00 0.00 Fixnum#abs
4.00 0.98 0.04 10000 0.00 0.00 Array#sample
2.00 1.00 0.02 20000 0.00 0.00 Kernel#respond_to_missing?
0.00 1.00 0.00 1 0.00 0.00 TracePoint#enable
0.00 1.00 0.00 1 0.00 0.00 MonitorMixin#mon_exit
0.00 1.00 0.00 1 0.00 0.00 Mutex#unlock
0.00 1.00 0.00 1 0.00 0.00 MonitorMixin#mon_check_owner
0.00 1.00 0.00 1 0.00 0.00 MonitorMixin#mon_enter
0.00 1.00 0.00 1 0.00 0.00 Mutex#lock
0.00 1.00 0.00 3 0.00 0.00 Thread.current
0.00 1.00 0.00 1 0.00 0.00 TracePoint#disable
0.00 1.00 0.00 1 0.00 1000.00 #toplevel
srand 0
WIDTH, HEIGHT = 10, 10
array = Array.new(HEIGHT){ [0]*WIDTH }
require "ruby-prof"
RubyProf.start
10000.times do
y, x = rand(HEIGHT), rand(WIDTH)
g = array[y][x] + [-1,+1].sample
array[y][x] = g unless [[y-1,x],[y+1,x],[y,x-1],[y,x+1]].any?{ |y, x|
y>=0 && y<HEIGHT && x>=0 && x<WIDTH && 1 < (array[y][x] - g).abs
}
end
result = RubyProf.stop
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)
Thread ID: 70100140233520
Fiber ID: 70100140080880
Total: 0.109966
Sort by: self_time
%self total self wait child calls name
32.07 0.044 0.035 0.000 0.008 10000 Array#each
26.74 0.110 0.029 0.000 0.081 1 Integer#times
15.32 0.024 0.017 0.000 0.007 20000 Kernel#rand
7.89 0.052 0.009 0.000 0.044 10000 Enumerable#any?
7.66 0.008 0.008 0.000 0.000 25500 Fixnum#abs
6.69 0.007 0.007 0.000 0.000 20000 Kernel#respond_to_missing?
3.61 0.004 0.004 0.000 0.000 10000 Array#sample
0.03 0.110 0.000 0.000 0.110 1 Global#[No method]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment