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

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