Skip to content

Instantly share code, notes, and snippets.

@stakach
Created March 20, 2013 12:07
Show Gist options
  • Save stakach/5204160 to your computer and use it in GitHub Desktop.
Save stakach/5204160 to your computer and use it in GitHub Desktop.
Fiber vs Thread using futures
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