Skip to content

Instantly share code, notes, and snippets.

@raggi
Created April 30, 2009 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save raggi/104213 to your computer and use it in GitHub Desktop.
Save raggi/104213 to your computer and use it in GitHub Desktop.
show the effects of stack size on ruby's performance
#!/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