Skip to content

Instantly share code, notes, and snippets.

@panthomakos
Created February 5, 2012 02:16
Show Gist options
  • Save panthomakos/1742042 to your computer and use it in GitHub Desktop.
Save panthomakos/1742042 to your computer and use it in GitHub Desktop.
Ruby - Threading and the GIL
require 'benchmark'
require 'mysql2'
x = Mysql2::Client.new
y = Mysql2::Client.new
Benchmark.bm do |b|
b.report('w/o') do
x.query("SELECT SLEEP(1)")
y.query("SELECT SLEEP(1)")
end
b.report('with') do
a = Thread.new{ x.query("SELECT SLEEP(1)") }
b = Thread.new{ y.query("SELECT SLEEP(1)") }
a.join
b.join
end
end
Benchmark.bm do |x|
x.report('w/o') do
10_000_000.times{ 2+2 }
end
x.report('with') do
a = Thread.new{ 5_000_000.times{ 2+2 } }
b = Thread.new{ 5_000_000.times{ 2+2 } }
a.join
b.join
end
end
Benchmark.bm do |x|
x.report('w/o') do
2.times{ sleep(1) }
end
x.report('with') do
a = Thread.new{ sleep(1) }
b = Thread.new{ sleep(1) }
a.join
b.join
end
end
user system total real
w/o 0.000000 0.000000 0.000000 ( 2.002883)
with 0.000000 0.000000 0.000000 ( 1.001794)
user system total real
w/o 2.470000 0.010000 2.480000 ( 2.472945)
with 2.400000 0.000000 2.400000 ( 2.414797)
user system total real
w/o 0.000000 0.000000 0.000000 ( 2.002112)
with 0.000000 0.000000 0.000000 ( 1.001252)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment