Created
April 28, 2021 20:28
-
-
Save sbpipb/534d6a5b556b4c2d23a62f9f140a70f2 to your computer and use it in GitHub Desktop.
code dump
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
# 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