Skip to content

Instantly share code, notes, and snippets.

@tjwallace
Created January 13, 2012 01:01

Revisions

  1. tjwallace created this gist Jan 13, 2012.
    19 changes: 19 additions & 0 deletions threads.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    require 'benchmark'
    require 'net/http'
    require 'uri'

    def network_read(uri)
    Net::HTTP.get_response uri
    end

    n = 100
    uri = URI('http://google.com/')

    Benchmark.bm(5) do |x|
    x.report('loop ') { n.times { network_read(uri) } }
    x.report('threads') {
    n.times.map {
    Thread.new { network_read(uri) }
    }.map(&:join)
    }
    end