Created
February 5, 2012 02:16
-
-
Save panthomakos/1742042 to your computer and use it in GitHub Desktop.
Ruby - Threading and the GIL
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 '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 |
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
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