Skip to content

Instantly share code, notes, and snippets.

@iHiD
Last active December 25, 2015 09:09
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 iHiD/6951575 to your computer and use it in GitHub Desktop.
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&…
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
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