Skip to content

Instantly share code, notes, and snippets.

@zw963
Last active April 10, 2021 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zw963/edcf22af743df46d7d8b63b552e78c26 to your computer and use it in GitHub Desktop.
Save zw963/edcf22af743df46d7d8b63b552e78c26 to your computer and use it in GitHub Desktop.
Ruby 3 benchmark for thread/process/Ractor
def tarai(x, y, z)
x <= y ? y : tarai(
tarai(x-1, y, z),
tarai(y-1, z, x),
tarai(z-1, x, y)
)
end
require 'benchmark'
Benchmark.bm do |x|
x.report('seq') do
4.times { tarai(14, 7, 0) }
end
end # => total: 83, real: 83
Benchmark.bm do |x|
x.report('thd') do
4.times.map do
Thread.new { tarai(14, 7, 0) }
end.each(&:join)
end
end # => total: 79, real: 79
Benchmark.bm do |x|
x.report('par') do
4.times.map do
Ractor.new { tarai(14, 7, 0) }
end.each(&:take)
end
end # => total: 114, real: 29
Benchmark.bm do |x|
x.report('fork') do
4.times.map do
Process.fork { tarai(14, 7, 0) }
end
Process.waitall
end
end # => total: 105, real: 26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment