Skip to content

Instantly share code, notes, and snippets.

@sbpipb
Created April 28, 2021 20:28
Show Gist options
  • Save sbpipb/534d6a5b556b4c2d23a62f9f140a70f2 to your computer and use it in GitHub Desktop.
Save sbpipb/534d6a5b556b4c2d23a62f9f140a70f2 to your computer and use it in GitHub Desktop.
code dump
# Concurrency and Parallelism
# https://www.toptal.com/ruby/ruby-concurrency-and-parallelism-a-practical-primer
require 'benchmark'
def fib(n)
n < 2 ? n : fib(n-1) + fib(n-2)
end
puts Benchmark.measure {
100.times do |x|
fib(30)
end
}
# forking multi process
puts Benchmark.measure {
100.times do |x|
fork do
fib(30)
end
end
Process.waitall
}
# multi threading
threads = []
puts Benchmark.measure{
100.times do |i|
threads << Thread.new do
fib(30)
end
end
threads.map(&:join)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment