Skip to content

Instantly share code, notes, and snippets.

@benwoody
Created August 31, 2012 01:04
Show Gist options
  • Save benwoody/3547074 to your computer and use it in GitHub Desktop.
Save benwoody/3547074 to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'benchmark'
urls = []
urls << "http://ruby-lang.org"
urls << "http://theplatform.com"
urls << "http://reddit.com"
urls << "http://pragprog.com"
urls << "http://railscasts.com"
def threaded(urls)
thread_list = []
urls.each do |f|
thread_list << Thread.new {
open(f) do |x|
x.read
end
puts "reading: " + f
}
end
thread_list.each {|x| x.join}
end
def sequential(urls)
urls.each do |f|
open(f) do |x|
x.read
end
puts "reading: " + f
end
end
Benchmark.bm do |x|
x.report("Threaded:\n") { threaded(urls) }
x.report("Sequential:\n") { sequential(urls) }
end
@benwoody
Copy link
Author

   user     system      total        real

Threaded:
reading: http://pragprog.com
reading: http://reddit.com
reading: http://railscasts.com
reading: http://theplatform.com
reading: http://ruby-lang.org
0.060000 0.030000 0.090000 ( 2.438356)
Sequential:
reading: http://ruby-lang.org
reading: http://theplatform.com
reading: http://reddit.com
reading: http://pragprog.com
reading: http://railscasts.com
0.040000 0.010000 0.050000 ( 4.286305)

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