require 'celluloid/current'
require 'benchmark'
class Integer
def fib
if self <= 1; self else ( self-1 ).fib + ( self-2 ).fib end
end
end
class FibWorker
include Celluloid
def do_fib i
i.fib
end
end
def fib_actor
pool = FibWorker.pool size: 8
fibs = []
38.times do | i | fibs << pool.future.do_fib(i) end
38.times do | i | fibs << pool.future.do_fib(i) end
38.times do | i | fibs << pool.future.do_fib(i) end
38.times do | i | fibs << pool.future.do_fib(i) end
fibs.each do |x| puts x.value end
end
def fib_no_actor
38.times do | i | puts i.fib end
38.times do | i | puts i.fib end
38.times do | i | puts i.fib end
38.times do | i | puts i.fib end
end
puts Benchmark.measure { fib_no_actor }
puts Benchmark.measure { fib_actor }