Created
March 20, 2013 11:47
-
-
Save stakach/5204063 to your computer and use it in GitHub Desktop.
Celluloid Actors vs Actor Pools - Fibres vs Threads
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' | |
$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