Skip to content

Instantly share code, notes, and snippets.

@damphyr
Created May 16, 2012 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damphyr/2710979 to your computer and use it in GitHub Desktop.
Save damphyr/2710979 to your computer and use it in GitHub Desktop.
Thread behaviour in Ruby (In the GIL's grip)
meisterbrau:sandbox riva$ ruby -v
jruby 1.6.7 (ruby-1.8.7-p357) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_31) [darwin-x86_64-java]
meisterbrau:sandbox riva$ ruby threading.rb
No threads: 0.955 seconds
Single thread: 0.667 seconds
Two threads: 0.683 seconds
meisterbrau:sandbox riva$ ruby -v
rubinius 2.0.0dev (1.8.7 40c0dc72 yyyy-mm-dd JI) [x86_64-apple-darwin10.8.0]
meisterbrau:sandbox riva$ ruby threading.rb
No threads: 1.269473 seconds
Single thread: 1.364589 seconds
Two threads: 0.41337900000000005 seconds
meisterbrau:sandbox riva$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [i686-darwin10.8.0]
meisterbrau:sandbox riva$ ruby threading.rb
No threads: 2.694062 seconds
Single thread: 2.676911 seconds
Two threads: 2.762391 seconds
meisterbrau:sandbox riva$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]
meisterbrau:sandbox riva$ ruby threading.rb
No threads: 0.980252 seconds
Single thread: 1.025127 seconds
Two threads: 0.976637 seconds
meisterbrau:sandbox riva$ ruby -v
ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin10.8.0]
meisterbrau:sandbox riva$ ruby threading.rb
No threads: 0.977271 seconds
Single thread: 0.994097 seconds
Two threads: 1.017854 seconds
repetitions = 10000000
half = repetitions/2
start_time = Time.now
a = 1
0.upto(repetitions) do |x|
a= a + x
end
end_time = Time.now
time_delta = end_time - start_time
puts "No threads: #{time_delta} seconds"
start_time = Time.now
t1 = Thread.new do
a = 1
0.upto(repetitions) do |x|
a= a + x
end
end
t1.join
end_time = Time.now
time_delta = end_time - start_time
puts "Single thread: #{time_delta} seconds"
start_time = Time.now
t1 = Thread.new do
a = 1
0.upto(half) do |x|
a= a + x
end
end
t2 = Thread.new do
a = 1
0.upto(half) do |x|
a= a + x
end
end
t2.join
t1.join
end_time = Time.now
time_delta = end_time - start_time
puts "Two threads: #{time_delta} seconds"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment