Skip to content

Instantly share code, notes, and snippets.

@Sixeight
Created May 15, 2009 09:59
Show Gist options
  • Save Sixeight/112153 to your computer and use it in GitHub Desktop.
Save Sixeight/112153 to your computer and use it in GitHub Desktop.
#! /usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'net/http'
require 'open-uri'
require 'benchmark'
require 'rubygems'
require 'hpricot'
require 'typhoeus'
words = 50.times.map { ('a'..'z').to_a.shuffle.take(rand(4) + 1).join }
res1 = res2 = res3 = nil
puts 'Search words;'
p words
puts
def get_titles(q)
(Hpricot(open("http://www.google.com/search?q=#{q}").read)/'title').text
end
class Remote; include Typhoeus end
Benchmark.bmbm do |x|
x.report do
res1 = words.map {|q| get_titles(q) }
end
x.report do
res2 = words.map {|q| Thread.new { get_titles(q) } }.map(&:value)
end
x.report do
res3 = words.map {|q|
Remote.get("http://www.google.com/search?q=#{q}")
}.map {|res|
(Hpricot(res.body)/'title').text
}
end
end
p res1 == res2 && res1 == res3 && res2 == res3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment