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 |
In the latest version of ruby (ruby/ruby@1ecda21), this performance issue has already resolved.
./miniruby -I./lib -I. -I.ext/common -W0 ./ractor.rb
user system total real
process 0.003058 0.000000 22.619907 ( 1.431280)
thread 13.808919 0.017577 13.826496 ( 13.821123)
ractor 22.718945 0.000037 22.718982 ( 1.447091)
Processing time was increased. It may be caused by compilation configuration.
https://bugs.ruby-lang.org/issues/17497
This issue has been discussed above.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My code may be wrong. Ractor is slowest. Why...?