Created
December 10, 2017 21:36
-
-
Save ioquatix/c8be70a7ca73bc30f73d3a0e551376cd to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'async' | |
require 'lightio' | |
def run_async(count = 10000) | |
start = Time.now | |
Async::Reactor.run do |task| | |
tasks = count.times.map do | |
# LightIO::Beam is a thread-like executor, use it instead Thread | |
task.async do |subtask| | |
# do some io operations in beam | |
subtask.sleep(1) | |
end | |
end | |
tasks.each(&:wait) | |
seconds = Time.now - start | |
puts "#{count} tasks take #{seconds - 1} seconds to create" | |
end | |
end | |
def run_lightio(count = 10000) | |
require 'lightio' | |
start = Time.now | |
beams = count.times.map do | |
# LightIO::Beam is a thread-like executor, use it instead Thread | |
LightIO::Beam.new do | |
# do some io operations in beam | |
LightIO.sleep(1) | |
end | |
end | |
beams.each(&:join) | |
seconds = Time.now - start | |
puts "#{count} beams take #{seconds - 1} seconds to create" | |
end | |
10.times do | |
run_lightio | |
run_async | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment