Created
August 31, 2012 03:55
-
-
Save benwoody/3548911 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'open-uri' | |
require 'benchmark' | |
require 'celluloid' | |
class Winning | |
include Celluloid | |
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") { Winning.new.threaded(urls) } | |
x.report("Sequential:\n") { Winning.new.sequential(urls) } | |
x.report("Celluloid:\n") { Winning.new.sequential!(urls) } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Threaded:
reading: http://reddit.com
reading: http://railscasts.com
reading: http://pragprog.com
reading: http://theplatform.com
reading: http://ruby-lang.org
0.060000 0.020000 0.080000 ( 1.419672)
Sequential:
reading: http://ruby-lang.org
reading: http://theplatform.com
reading: http://reddit.com
reading: http://pragprog.com
reading: http://railscasts.com
0.050000 0.020000 0.070000 ( 4.242443)
Celluloid:
0.000000 0.000000 0.000000 ( 0.000517) # Hmmm... due to actors being spun up, not process time
I, [2012-08-30T20:50:50.877574 #6790] INFO -- : Terminating 4 actors...
reading: http://ruby-lang.org
reading: http://theplatform.com
reading: http://reddit.com
reading: http://pragprog.com
reading: http://railscasts.com
I, [2012-08-30T20:50:56.378560 #6790] INFO -- : Shutdown completed cleanly