Created
July 6, 2022 15:14
-
-
Save forsaken1/600859d5a3ad3e2fdf8765db04117484 to your computer and use it in GitHub Desktop.
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
require 'benchmark' | |
ACTORS_COUNT = 8 | |
THREADS_COUNT = 8 | |
def iterative_fib(num) | |
a = 0 | |
b = 1 | |
num.times do | |
temp = a | |
a = b | |
b = temp + b | |
end | |
return a | |
end | |
def smth_heavy | |
iterative_fib(1000000) | |
end | |
def paralelly | |
actors = Array.new ACTORS_COUNT do | |
Ractor.new { smth_heavy } | |
end | |
actors.map &:take | |
end | |
def sequentally | |
threads = Array.new THREADS_COUNT do | |
Thread.new { smth_heavy } | |
end | |
threads.map &:join | |
end | |
Benchmark.bm do |x| | |
x.report { paralelly } | |
x.report { sequentally } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment