Created
January 31, 2021 01:39
-
-
Save ytnk531/edead9655ebdcde7d0273db941ec43ae to your computer and use it in GitHub Desktop.
Ruby parallel execution benchmark.
This file contains 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
PARALLELISM = 16 | |
def solve | |
(1..5_000_000).inject(:+) | |
end | |
def solve_ractor | |
ractors = PARALLELISM.times.map { Ractor.new { solve } } | |
ractors.each { Ractor.select _1 } | |
end | |
def solve_thread | |
threads = PARALLELISM.times.map { Thread.new { solve } } | |
threads.each { _1.join } | |
end | |
def solve_process | |
pids = PARALLELISM.times.map { fork { solve } } | |
pids.each { Process.waitpid _1 } | |
end | |
require "benchmark" | |
Benchmark.bm do |x| | |
x.report("process") { solve_process } | |
x.report("thread") { solve_thread } | |
x.report("ractor") { solve_ractor } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the latest version of ruby (ruby/ruby@1ecda21), this performance issue has already resolved.
Processing time was increased. It may be caused by compilation configuration.