Last active
December 25, 2015 09:09
-
-
Save iHiD/6951575 to your computer and use it in GitHub Desktop.
Event Machine Test - Checking performance between EventMachine and the underlying Threads. My results show that for 1.9.3, EventMachine executes MUCH faster than the just using Thread.new. However, for 2.0.0 and Rubinius, using Thread.new seems to be the same or marginally faster. https://github.com/eventmachine/eventmachine/search?q=Thread.new&…
This file contains hidden or 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 'eventmachine' | |
def write_file(text) | |
filename = "/Users/iHiD/Desktop/dump.txt" | |
File.open(filename, 'w') do |f| | |
f.flock(File::LOCK_EX) | |
f.write(text) | |
sleep 0.005 | |
end | |
end | |
speed = 400 | |
fast_proc = Proc.new { speed.times {|i| write_file(i) } } | |
slow_proc = Proc.new { (speed * 1.1).to_i.times {|i| write_file(i) } } | |
EventMachine.run do | |
EM.defer do | |
puts "EM 1" | |
fast_proc.call | |
puts "EM 3" | |
end | |
EM.defer do | |
puts "EM 2" | |
slow_proc.call | |
puts "EM 4" | |
end | |
Thread.new do | |
puts "Thread 1" | |
fast_proc.call | |
puts "Thread 3" | |
end | |
Thread.new do | |
puts "Thread 2" | |
slow_proc.call | |
puts "Thread 4" | |
end | |
end |
This file contains hidden or 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 'eventmachine' | |
EventMachine.run do | |
EM.defer do | |
puts "EM 1" | |
i = 0 | |
20000000.times{ i += 1 } | |
puts "EM 3" | |
end | |
EM.defer do | |
puts "EM 2" | |
i = 0 | |
30000000.times{ i += 1 } | |
puts "EM 4" | |
end | |
Thread.new do | |
puts "Thread 1" | |
i = 0 | |
20000000.times{ i += 1 } | |
puts "Thread 3" | |
end | |
Thread.new do | |
puts "Thread 2" | |
i = 0 | |
30000000.times{ i += 1 } | |
puts "Thread 4" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment