Created
March 20, 2013 12:07
-
-
Save stakach/5204160 to your computer and use it in GitHub Desktop.
Fiber vs Thread using futures
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 'celluloid' | |
require 'benchmark' | |
class Example | |
include Celluloid | |
def slow_method | |
sleep 5 | |
end | |
def fast_method | |
'so fast' | |
end | |
end | |
single = Example.new | |
pool = Example.pool | |
tasks = ::Celluloid.cores * 2 | |
time = Benchmark.measure do | |
tasks.times { single.async.slow_method } | |
future = single.future.fast_method | |
future.value | |
end | |
p "single executed in #{time}" | |
sleep 10 # ensures nothing processing | |
time = Benchmark.measure do | |
tasks.times { pool.async.slow_method } | |
future = pool.future.fast_method | |
future.value | |
end | |
p "pool executed in #{time}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment