It’s multi-core age today. Concurrency is very important. With Ractor, along with Async Fiber, Ruby will be a real concurrent language. — Matz
文档中提及在 4 核心并行 处理的时候,性能是单核处理的 3.87 倍
def tarai(x, y, z) =
x <= y ? y : tarai(tarai(x-1, y, z),
tarai(y-1, z, x),
tarai(z-1, x, y))
require 'benchmark'
Benchmark.bm do |x|
# sequential version
x.report('seq'){ 4.times{ tarai(14, 7, 0) } }
# parallel version
x.report('par'){
4.times.map do
Ractor.new { tarai(14, 7, 0) }
end.each(&:take)
}
end
Benchmark result:
user system total real
seq 64.560736 0.001101 64.561837 ( 64.562194)
par 66.422010 0.015999 66.438009 ( 16.685797)
The result was measured on Ubuntu 20.04,
Intel(R) Core(TM) i7-6700 (4 cores, 8 hardware threads).
It shows that the parallel version is 3.87 times faster than the sequential version.
然后又看了 ractor 的文档
从标红的地方来看,在同一个 ractor
内的线程因为有 Ractor-wide global lock
类似 GIL 锁 不能实现多核并发,但多个 ractor
之前是可以实现多核并发
实测了一下,有效果
def tarai(x, y, z)
x <= y ? y : tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y))
end
require "benchmark"
Benchmark.bm do |x|
x.report('Ractor') { 6.times.map { Ractor.new { tarai(14, 7, 0) } }.each(&:take) }
end
我调整参数以后,基于最新的 Ruby 3.0.0 跑,把 MacBook 的 6 核 CPU 全部用上了:
如果是 Ruby 2.7 用 Thread:
def tarai(x, y, z)
x <= y ? y : tarai(tarai(x - 1, y, z), tarai(y - 1, z, x), tarai(z - 1, x, y))
end
require "benchmark"
Benchmark.bm do |x|
x.report("Thread") { 6.times.map { Thread.new { tarai(14, 7, 0) } }.each(&:join) }
end
有很多吗??
https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/
The following libraries are no longer bundled gems or standard libraries. Install the corresponding gems to use these features.
sdbm
webrick
net-telnet
xmlrpc
macOS Big Sur 默认的版本才 ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin20] 什么时候可以默认安装到 3.0.0