Skip to content

Instantly share code, notes, and snippets.

@chuckremes
Last active January 11, 2018 00:22
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 chuckremes/a917878311528d26b333db299f6a128d to your computer and use it in GitHub Desktop.
Save chuckremes/a917878311528d26b333db299f6a128d to your computer and use it in GitHub Desktop.
ruby 2.5.0
Comparison:
sync each, buffered: 104.9 i/s
async each, buffered: 96.6 i/s - same-ish: difference falls within error
async each, unbuffered: 95.7 i/s - same-ish: difference falls within error
sync each, unbuffered: 22.1 i/s - 4.74x slower
rubinius 3.90
Comparison:
async each, buffered: 2.5 i/s
async each, unbuffered: 2.5 i/s - 1.02x slower
sync each, buffered: 2.4 i/s - 1.08x slower
sync each, unbuffered: 0.5 i/s - 5.34x slower
$: << File.join(File.dirname(__FILE__), '../lib')
require 'io'
require 'benchmark/ips'
# Setup test file on filesystem outside timing
file_path = File.join(File.dirname(__FILE__), 'fixtures', 'ascii_0_9_small.txt')
flags = IO::Config::Flags.new
sync_io_unbuffered = IO::Sync::File.open(
path: file_path,
flags: flags.readonly
)
sync_io_unbuffered.extend(IO::Mixins::UnbufferedEnumerable)
sync_io_buffered = IO::Sync::File.open(
path: file_path,
flags: flags.readonly
)
sync_io_buffered.extend(IO::Mixins::Enumerable)
async_io_unbuffered = IO::Async::File.open(
path: file_path,
flags: flags.readonly
)
async_io_unbuffered.extend(IO::Mixins::UnbufferedEnumerable)
async_io_buffered = IO::Async::File.open(
path: file_path,
flags: flags.readonly
)
async_io_buffered.extend(IO::Mixins::Enumerable)
regular_ruby = File.open(
file_path,
'r'
)
newio_iterations = 0
newio_times = 0
nativeio_iterations = 0
nativeio_times = 0
Benchmark.ips do |x|
# Configure the number of seconds used during
# the warmup phase (default 2) and calculation phase (default 5)
x.config(:time => 15, :warmup => 5)
x.report("sync each, buffered") do |times|
newio_times += times
i = 0
while i < times
sync_io_buffered.each(limit: 5) do |rc, errno, str, offset|
# no op
newio_iterations += 1
end
i += 1
end
end
x.report("sync each, unbuffered") do |times|
i = 0
while i < times
sync_io_unbuffered.each(limit: 5) do |rc, errno, str, offset|
raise "read error, rc [#{rc}], errno [#{errno}]" if rc < 0
# no op
end
i += 1
end
end
x.report("async each, buffered") do |times|
i = 0
while i < times
async_io_buffered.each(limit: 5) do |rc, errno, str, offset|
raise "read error, rc [#{rc}], errno [#{errno}]" if rc < 0
# no op
end
i += 1
end
end
x.report("async each, unbuffered") do |times|
i = 0
while i < times
async_io_buffered.each(limit: 5) do |rc, errno, str, offset|
raise "read error, rc [#{rc}], errno [#{errno}]" if rc < 0
# no op
end
i += 1
end
end
# x.report("sync each, native IO") do |times|
# nativeio_times += times
# i = 0
# while i < times
# regular_ruby.rewind
# regular_ruby.each(5) do |line|
# nativeio_iterations += 1
# raise "read error, regular ruby" if line.size < 0
# # no op
# end
# i += 1
# end
# end
# Compare the iterations per second of the various reports!
x.compare!
end
puts "newio_iterations [#{newio_iterations}], nativeio_iterations [#{nativeio_iterations}]"
puts "newio_times [#{newio_times}], nativeio_times [#{nativeio_times}]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment