Ruby 3, Concurrency and the Ecosystem
Some silly multi threaded questions incoming.<p>I ran a quick benchmark here is the code<p><pre><code>  require &#x27;benchmark&#x27;
  require &#x27;prime&#x27;
  Benchmark.bm do |x|

  x.report(&#x27;single&#x27;){
    8.times do 
      10_000_000.times.each do |num|
        num.prime?
      end
    end
  }

  x.report(&#x27;thread&#x27;){ 
    8.times.map do 
      Thread.new do 
        10_000_000.times.each do |num|
          num.prime?
        end
      end
    end.each(&amp;:join)
  }

  x.report(&#x27;parallel&#x27;){
    8.times.map do
      Ractor.new do 
        10_000_000.times.each do |num|
          num.prime?
        end
      end
    end.each(&amp;:take)
    }
  end
</code></pre>
results<p>single -&gt; 121.66s<p>thread -&gt; 122.14s<p>parallel -&gt; 52.84s<p>The single and threaded version were the same and this is mostly expected since we don&#x27;t have any I&#x2F;O in the threads which would of given up the thread and scheduled another to be run.<p>For prime calculations, we are just bound by pure CPU and limited by ruby&#x27;s global interpreter lock I assume this would be the same case in Python.<p>1) A silly question and more language irrelevant, when looking at my CPU logical core usage during the single and fake threaded version I could see tiny spikes in all the cores. [0] No one core dominated the task how does this work? Does the CPU know to workshare single threaded tasks and branch prediction etc comes into play here? How are modern CPU&#x27;s achieving this at a lower level? In my head this task would just spike a single core to do all the work.<p>2) Running the 10_00_000 primes just once completes in 15.3s shouldn&#x27;t this be the same amount of time for 80_00_000 if the Ractor implementation was optimized across my 8 logical cores? Not a punch or anything at guys working on lang its freaking awesome to see my CPU being maxed out with my 8 logical processors being used in Ruby! [1] but just want to double check my understanding.<p>[0] <a href="https:&#x2F;&#x2F;postimg.cc&#x2F;rKjB9kQP" rel="nofollow">https:&#x2F;&#x2F;postimg.cc&#x2F;rKjB9kQP</a>
[1] <a href="https:&#x2F;&#x2F;postimg.cc&#x2F;WtvpWswN" rel="nofollow">https:&#x2F;&#x2F;postimg.cc&#x2F;WtvpWswN</a>