Skip to content

Instantly share code, notes, and snippets.

@ioquatix
Created May 17, 2020 05:04
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/0b61246e91f94071d5c8c6f9b68e6107 to your computer and use it in GitHub Desktop.
Save ioquatix/0b61246e91f94071d5c8c6f9b68e6107 to your computer and use it in GitHub Desktop.
Threads vs Processes with the GVL
# gem install async-container
gem "async-container"
require 'async/clock'
require 'async/container'
def fibonacci(n)
if n < 2
return n
else
return fibonacci(n-1) + fibonacci(n-2)
end
end
def work(*)
puts fibonacci(32)
end
def measure_work(container, **options, &block)
duration = Async::Clock.measure do
container.run(**options, &block)
container.wait
end
puts "Duration for #{container.class}: #{duration}"
end
threaded = Async::Container::Threaded.new
measure_work(threaded, count: 20, &self.method(:work))
forked = Async::Container::Forked.new
measure_work(forked, count: 20, &self.method(:work))
@ioquatix
Copy link
Author

ioquatix commented May 17, 2020

Here is a work for being I/O bound:

def work
	512.times do
		File.read("/dev/zero", 1024*1024).bytesize
	end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment