Skip to content

Instantly share code, notes, and snippets.

@ytnk531
Created January 31, 2021 01:39
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 ytnk531/edead9655ebdcde7d0273db941ec43ae to your computer and use it in GitHub Desktop.
Save ytnk531/edead9655ebdcde7d0273db941ec43ae to your computer and use it in GitHub Desktop.
Ruby parallel execution benchmark.
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
@ytnk531
Copy link
Author

ytnk531 commented Jan 31, 2021

My code may be wrong. Ractor is slowest. Why...?

ruby -W0 ractor.rb
       user     system      total        real
process  0.000000   0.007675   5.502588 (  0.352230)
thread  2.771559   0.022077   2.793636 (  2.791870)
ractor 14.277291  99.586723 113.864014 (  7.301255)

@ytnk531
Copy link
Author

ytnk531 commented Jan 31, 2021

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.

@ytnk531
Copy link
Author

ytnk531 commented Jan 31, 2021

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