Skip to content

Instantly share code, notes, and snippets.

@forsaken1
Created July 6, 2022 15:14
Show Gist options
  • Save forsaken1/600859d5a3ad3e2fdf8765db04117484 to your computer and use it in GitHub Desktop.
Save forsaken1/600859d5a3ad3e2fdf8765db04117484 to your computer and use it in GitHub Desktop.
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