Last active
August 29, 2015 14:01
-
-
Save bewatts/d1e3661c7c1ccfa01355 to your computer and use it in GitHub Desktop.
NokoGiri selectors vs Ruby loops
This file contains 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 'benchmark' | |
require 'nokogiri' | |
require 'open-uri' | |
class PageScraper | |
def self.get_youtube_urls(url) | |
self.new(url).get_youtube_urls | |
end | |
def initialize(url) | |
@doc = Nokogiri::HTML(open(url)) | |
end | |
def get_youtube_urls | |
youtube_page_links.map{|link| link.attribute('href').to_s} | |
end | |
def find_css_elements(selector = 'a') | |
@doc.css(selector) | |
end | |
end | |
class PageScraperNoko < PageScraper | |
def youtube_page_links | |
find_css_elements('a[href*="youtu"]') | |
end | |
end | |
class PageScraperRuby < PageScraper | |
def get_youtube_urls | |
youtube_page_links.map{|link| link.attribute('href').to_s}. | |
delete_if {|href| !(/youtu/.match(href)) } | |
end | |
def youtube_page_links | |
find_css_elements('a') | |
end | |
end | |
URL = "http://ask.metafilter.com/252306/Looking-For-Songs-That-Have-A-Dark-Driving-Beat" | |
iterations = 100 | |
Benchmark.bm do |bm| | |
bm.report do | |
iterations.times do | |
PageScraperNoko.get_youtube_urls(URL) | |
end | |
end | |
bm.report do | |
iterations.times do | |
PageScraperRuby.get_youtube_urls(URL) | |
end | |
end | |
end | |
# results | |
# user system total real | |
# 1.060000 0.120000 1.180000 ( 21.660606) | |
# 1.000000 0.110000 1.110000 ( 19.030504) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment