Tony Arcieri wrote:
> Ruby 1.9 isn't going to help you when using threads to distribute
> computation across CPU cores.  The Global VM Lock ensures that
> simultaneous computation is still limited to one core.
> 
> JRuby, on the other hand, does not have this limitation.  On MRI/1.9
> I would recommend using multiple processes.

I'm not so sure jruby does this effectively.

require 'tiamat/autoconfig'
require 'pure/dsl'
require 'benchmark'

mod = pure do
  def total(left, right)
    left + right
  end

  def left
    (1..5_000_000).inject(0) { |acc, n| acc + n }
  end

  def right
    (1..5_000_000).inject(0) { |acc, n| acc + n }
  end
end

Benchmark.bmbm { |bm|
  bm.report("1 thread, 1 interpreter") {
    mod.compute(1).total
  }
  bm.report("2 threads, 1 interpreter") {
    mod.compute(2).total
  }
  # this part removed for jruby bench
  bm.report("2 threads, 2 interpreters") {
    Tiamat.open_local(2) {
      mod.compute.total
    }
  }
}

== ruby 1.9.2dev (2009-10-18 trunk 25393) [i386-darwin9.8.0]
Rehearsal -------------------------------------------------------------
1 thread, 1 interpreter     4.370000   0.020000   4.390000 (  4.389990)
2 threads, 1 interpreter    4.360000   0.030000   4.390000 (  4.385111)
2 threads, 2 interpreters   0.010000   0.010000   4.700000 (  2.460661)
--------------------------------------------------- total: 13.480000sec

                                user     system      total        real
1 thread, 1 interpreter     4.360000   0.020000   4.380000 (  4.376050)
2 threads, 1 interpreter    4.360000   0.030000   4.390000 (  4.380982)
2 threads, 2 interpreters   0.010000   0.010000   4.710000 (  2.465925)


== jruby 1.4.0RC3 (ruby 1.8.7 patchlevel 174) (2009-10-30 1d7de2d) (Java 
HotSpot(TM) Client VM 1.5.0_20) [i386-java]
Rehearsal ------------------------------------------------------------
1 thread, 1 interpreter    6.060000   0.000000   6.060000 (  6.060000)
2 threads, 1 interpreter   7.629000   0.000000   7.629000 (  7.629000)
-------------------------------------------------- total: 13.689000sec

                               user     system      total        real
1 thread, 1 interpreter    6.080000   0.000000   6.080000 (  6.080000)
2 threads, 1 interpreter   7.288000   0.000000   7.288000 (  7.288000)


-- 
Posted via http://www.ruby-forum.com/.