Created
April 30, 2009 02:40
-
-
Save raggi/104213 to your computer and use it in GitHub Desktop.
show the effects of stack size on ruby's performance
This file contains hidden or 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
#!/usr/bin/env ruby | |
require "benchmark" | |
bit64 = 1.size == 8 | |
open('gdb-script', 'w') do |f| | |
f.puts <<-EOS | |
p ((unsigned int)rb_gc_stack_start - (unsigned int)$#{bit64 ? 'r' : 'e'}sp) | |
detach | |
quit | |
EOS | |
end | |
def stack(size = 200, n = 0, &blk) | |
unless n >= size | |
stack(size, n+1, &blk) | |
else | |
yield | |
end | |
end | |
def run_test | |
0.step(500, 100) do |size| | |
stack(size) do | |
ss = %x"gdb -q -x gdb-script `which ruby` #{$$} 2>/dev/null"[/\$1 = (\d+)/m, 1] | |
puts "Ruby stack: #{"%4d" % size} Stack size: #{"%10d" % ss} bytes" | |
puts(Benchmark.measure { 10_000.times { stack(10) { ss.size } } }) | |
end | |
end | |
end | |
puts "Without thread:" | |
run_test | |
puts | |
puts "With stopped thread:" | |
t1 = Thread.new { Thread.stop } | |
run_test | |
puts | |
t2 = Thread.new { loop { stack(10) { Thread.current } } } | |
puts "With active thread: (n.b. stack size lies!)" | |
run_test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment