2 ms·
It ain't pretty, but: require 'mechanize' class Tinyurl def self.make_tiny(big_url) url = "http://tinyurl.com/api-create.php?url=#{big_url}" agen
by sanj 18y ago
It ain't pretty, but:
require 'mechanize'
class Tinyurl
def self.make_tiny(big_url)
url = "http://tinyurl.com/api-create.php?url=#{big_url}"
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Mac Safari'
results = agent.get(url)
return results.body
end
def self.test(base="http://www.google.com?q=sanjay", iterations = 10)
t=Time.now
(0..iterations).to_a.each do |i|
puts make_tiny("#{base}#{i}:#{rand(100000000)}")
end
puts Time.now-t
end
end
Tinyurl.test
- mncaudill 18y agoI could be wrong as I'm not that familiar with Ruby, but I imagine to get more meaningful results (not saying that the results will be different), I would run the tests many more times than 10, as well as not printing to stdout inside of your testing loop.
- sanj 18y ago100 iterations instead of 10, removed puts: TinyURL: 0.43s/req bit.ly: 0.22s/req is.gd: 0.05s/req That's close enough to my original values to not matter. It appears that the time is all spent waiting for the site to return.