Skip to content

Instantly share code, notes, and snippets.

@stakach
Created March 20, 2013 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stakach/5204063 to your computer and use it in GitHub Desktop.
Save stakach/5204063 to your computer and use it in GitHub Desktop.
Celluloid Actors vs Actor Pools - Fibres vs Threads
require 'celluloid'
require 'benchmark'
$mutex = Mutex.new
$resource = ConditionVariable.new
class Example
include Celluloid
def slow_method
sleep 5
end
def fast_method
$mutex.synchronize {
$resource.signal
}
end
end
single = Example.new
pool = Example.pool
tasks = ::Celluloid.cores * 2
time = Benchmark.measure do
$mutex.synchronize {
tasks.times { single.async.slow_method }
single.async.fast_method
$resource.wait($mutex)
}
end
p "single executed in #{time}"
sleep 10 # ensures nothing processing
time = Benchmark.measure do
$mutex.synchronize {
tasks.times { pool.async.slow_method }
pool.async.fast_method
$resource.wait($mutex)
}
end
p "pool executed in #{time}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment