Last active
December 16, 2015 07:48
-
-
Save NateBarnes/5401122 to your computer and use it in GitHub Desktop.
Just a quick example of parallelizing network requests to improve SoA API call times to multiple endpoints using future methods.
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
require "httparty" | |
require "celluloid" | |
start_time = Time.now | |
results = [] | |
100.times do | |
results << HTTParty.get("http://www.reddit.com/r/ruby") | |
end | |
end_time = Time.now | |
puts "Procedural: #{end_time-start_time}" | |
start_time = Time.now | |
methods = [] | |
results = [] | |
100.times do | |
methods << Celluloid::Future.new { HTTParty.get("http://www.reddit.com/r/ruby") } | |
end | |
methods.each do |meth| | |
results << meth.value | |
end | |
end_time = Time.now | |
puts "Parallel: #{end_time-start_time}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment