-
Book Overview & Buying
-
Table Of Contents
Polished Ruby Programming - Second Edition
By :
To examine different concurrency approaches for CPU-bound applications, we are going to use a simple recursive Fibonacci method:
def fib(n)
if n < 2
1
else
fib(n-2) + fib(n-1)
end
end
This is an inefficient way to calculate Fibonacci, to put it mildly, but since we are only trying to generate CPU load to evaluate different concurrency approaches, it serves our purpose.
In general, the ideal number of CPU-bound tasks to execute in parallel is equal to the number of logical processors in the system. Ruby's etc standard library makes it easy to get the number of logical processors via Etc.nprocessors. However, be aware that this may report more processors than are actually usable in some cases if you are using a virtual machine:
require 'etc'
ncpu = Etc.nprocessors
For the Fibonacci calculation to yield a good distribution of work for tasks, it's helpful to have tasks of the same difficulty for the size of the task pool...
Change the font size
Change margin width
Change background colour