Skip to content

Instantly share code, notes, and snippets.

@slawosz
Created July 22, 2013 17:48
Show Gist options
  • Save slawosz/6055961 to your computer and use it in GitHub Desktop.
Save slawosz/6055961 to your computer and use it in GitHub Desktop.
➜ threads cat read_nonblock.rb
require 'benchmark'
result = Benchmark.measure do
(1..4).map { |i|
f = File.open("tmp#{i}.txt", 'w')
1.upto(100_000) do
f.write_nonblock('a' * 100) rescue nil
end
}
end
result2 = Benchmark.measure do
(1..4).map { |i|
Thread.new do
f = File.open("tmp#{i}.txt", 'w')
1.upto(100_000) do
f.write_nonblock('a' * 100) rescue nil
end
end
}.each(&:join)
end
puts 'non'
puts result
puts 'threaded'
puts result2
➜ threads cat read_block.rb
require 'benchmark'
result2 = Benchmark.measure do
(1..8).map { |i|
f = File.open("tmp#{i}.txt", 'w')
1.upto(100_000) do
f.write('a' * 100) rescue nil
end
}
end
result = Benchmark.measure do
(1..8).map { |i|
Thread.new do
f = File.open("tmp#{i}.txt", 'w')
1.upto(100_000) do
f.write('a' * 100) rescue nil
end
end
}.each(&:join)
end
puts 'Threaded:'
puts result
puts 'Non threaded 2:'
puts result2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment