Skip to content

Instantly share code, notes, and snippets.

@tarcieri
Created May 11, 2011 06:28
Show Gist options
  • Save tarcieri/966007 to your computer and use it in GitHub Desktop.
Save tarcieri/966007 to your computer and use it in GitHub Desktop.
Concurrent HTTP fetching benchmark with Enumerable#pmap
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
Copy link

dinks commented Sep 25, 2013

require 'celluloid'
require 'open-uri'
require 'cgi'
require 'benchmark'

module Enumerable
  def pmap(&block)
    futures = map { |elem| Celluloid::Future.new(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=([^\s&]+)+/)[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
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment