Created
December 31, 2021 01:00
-
-
Save ioquatix/07ed614aa63309e1a455e8bb01966bf4 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 'benchmark' | |
class Reading | |
def initialize | |
@r, @w = IO.pipe | |
@thread = Thread.new do | |
@r.read | |
rescue IOError | |
# Ignore. | |
end | |
end | |
attr :r | |
attr :w | |
attr :thread | |
def join | |
@thread.join | |
end | |
end | |
count = ARGV.last.to_i | |
# count = 10, average time to close = 0.06ms | |
# count = 10_000, average time to close = 1.2ms | |
readings = count.times.map do | |
Reading.new | |
end | |
sleep 0.1 | |
duration = Benchmark.measure do | |
readings.each do |reading| | |
reading.r.close | |
reading.w.close | |
end | |
end | |
pp duration: duration, average: (duration/count) | |
readings.each(&:join) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment