Friday, 7 February 2014

Full concurrency during your I/O calls

#gem install fibur
#And the lib its just :
#Fibur = Thread
#https://github.com/tenderlove/fibur/blob/master/lib/fibur.rb
#:)
require 'benchmark'
require 'net/http'
require 'uri'
require 'fibur' # use the Fibur gem.
def network_read(uri)
Net::HTTP.get_response uri
end
n = 50000
uri = URI('http://google.com/')
Benchmark.bm(5) do |x|
x.report('loop') { 100.times { network_read(uri) } }
x.report('fibur') {
100.times.map {
Fibur.new { network_read(uri) }
}.map(&:join)
}
end
#➜ Desktop ruby fibur.rb
# user system total real
#loop 0.120000 0.050000 0.170000 ( 23.593217)
#fibur 0.180000 0.060000 0.240000 ( 0.721610)
view raw gistfile1.rb hosted with ❤ by GitHub

No comments:

Post a Comment