Skip to content

Instantly share code, notes, and snippets.

@ioquatix
Created December 31, 2021 01:00
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/07ed614aa63309e1a455e8bb01966bf4 to your computer and use it in GitHub Desktop.
Save ioquatix/07ed614aa63309e1a455e8bb01966bf4 to your computer and use it in GitHub Desktop.
#!/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