Quantcast
Channel: Does ruby have real multithreading? - Stack Overflow
Viewing all articles
Browse latest Browse all 10

Answer by GroovyCakes for Does ruby have real multithreading?

$
0
0

I'll let the "System Monitor" answer this question. I'm executing the same code (below, which calculates prime numbers) with 8 Ruby threads running on an i7 (4 hyperthreaded-core) machine in both cases... the first run is with:

jruby 1.5.6 (ruby 1.8.7 patchlevel 249) (2014-02-03 6586)(OpenJDK 64-Bit Server VM 1.7.0_75) [amd64-java]

The second is with:

ruby 2.1.2p95 (2014-05-08) [x86_64-linux-gnu]

Interestingly, the CPU is higher for JRuby threads, but the time to completion is slightly shorter for the interpreted Ruby. It's kind of difficult to tell from the graph, but the second (interpreted Ruby) run uses about 1/2 the CPUs (no hyperthreading?)

enter image description here

def eratosthenes(n)  nums = [nil, nil, *2..n]  (2..Math.sqrt(n)).each do |i|    (i**2..n).step(i){|m| nums[m] = nil}  if nums[i]  end  nums.compactendMAX_PRIME=10000000THREADS=8threads = []1.upto(THREADS) do |num|  puts "Starting thread #{num}"  threads[num]=Thread.new { eratosthenes MAX_PRIME }end1.upto(THREADS) do |num|    threads[num].joinend

Viewing all articles
Browse latest Browse all 10

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>