Skip to content

Instantly share code, notes, and snippets.

@ioquatix
Created December 10, 2017 21:36
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 ioquatix/c8be70a7ca73bc30f73d3a0e551376cd to your computer and use it in GitHub Desktop.
Save ioquatix/c8be70a7ca73bc30f73d3a0e551376cd to your computer and use it in GitHub Desktop.
#!/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