Last active
April 10, 2021 04:37
-
-
Save zw963/edcf22af743df46d7d8b63b552e78c26 to your computer and use it in GitHub Desktop.
Ruby 3 benchmark for thread/process/Ractor
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
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