Created
May 11, 2011 06:28
-
-
Save tarcieri/966007 to your computer and use it in GitHub Desktop.
Concurrent HTTP fetching benchmark with Enumerable#pmap
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 'celluloid' | |
require 'open-uri' | |
require 'cgi' | |
require 'benchmark' | |
module Enumerable | |
def pmap(&block) | |
futures = map { |elem| Celluloid::Future(elem, &block) } | |
futures.map { |future| future.value } | |
end | |
end | |
def image_url(color, term) | |
url = "http://www.google.com/search?q=#{term}&tbm=isch&tbs=ic:specific,isc:#{color}" | |
page = open(url) { |sock| sock.read } | |
CGI.unescape page.match(/imgurl\\x3d(.+?)\\x26/)[1] | |
end | |
term = 'dog' | |
colors = %w(red green yellow blue) | |
require 'celluloid' | |
require 'open-uri' | |
require 'cgi' | |
require 'benchmark' | |
module Enumerable | |
def pmap(&block) | |
futures = map { |elem| Celluloid::Future(elem, &block) } | |
futures.map { |future| future.value } | |
end | |
end | |
def image_url(color, term) | |
url = "http://www.google.com/search?q=#{term}&tbm=isch&tbs=ic:specific,isc:#{color}" | |
page = open(url) { |sock| sock.read } | |
CGI.unescape page.match(/imgurl\\x3d(.+?)\\x26/)[1] | |
end | |
term = 'dog' | |
colors = %w(red green yellow blue) | |
puts Benchmark.measure { | |
urls = colors.map { |color| image_url color, term } | |
p urls | |
} | |
puts Benchmark.measure { | |
urls = colors.pmap { |color| image_url color, term } | |
p urls | |
} |
dinks
commented
Sep 25, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment