Skip to content

Instantly share code, notes, and snippets.

@RunsFor
Last active February 1, 2016 22:28
Show Gist options
  • Select an option

  • Save RunsFor/278bb83f18d6e85c1d67 to your computer and use it in GitHub Desktop.

Select an option

Save RunsFor/278bb83f18d6e85c1d67 to your computer and use it in GitHub Desktop.
Multithreaded script performs slower than singlethreaded
require 'benchmark'
puts(Benchmark.measure do
count = 0
400_000_000.times do
count += 1
end
end)
puts(Benchmark.measure do
(1..4).map do
Thread.new do
count = 0
100_000_000.times do
count += 1
end
end
end.each(&:join)
end)
@RunsFor

RunsFor commented Feb 1, 2016

Copy link
Copy Markdown
Author
⇒  ruby -v multithreaded.rb
jruby 9.0.4.0 (2.2.2) 2015-11-12 b9fb7aa Java HotSpot(TM) 64-Bit Server VM 25.72-b15 on 1.8.0_72-b15 +jit [darwin-x86_64]
 12.490000   0.120000  12.610000 ( 11.768930)
 64.580000   0.100000  64.680000 ( 17.294624)
⇒  ruby -v multithreaded.rb
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
 19.880000   0.010000  19.890000 ( 19.905881)
 21.120000   0.110000  21.230000 ( 21.348419)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment