Rus*_*nov 1 ruby performance multithreading ruby-on-rails
我目前正在Ruby中测试Threads,但是不明白:如果他们实际上是在MRI中工作?通过工作线程,我的意思是能够并行执行它们以加速应用程序.
这是我的例子:
require 'benchmark'
Benchmark.bm do |x|
x.report do
threads = []
thread_1 = Thread.new { (1..50_000_000).inject { |sum, n| sum + n } }
thread_2 = Thread.new { (1..100_000_000).inject { |sum, n| sum + n } }
threads << thread_1
threads << thread_2
threads.each { |t| t.join }
end
end
Benchmark.bm do |x|
x.report do
(1..50_000_000).inject { |sum, n| sum + n }
(1..100_000_000).inject { |sum, n| sum + n }
end
end
Run Code Online (Sandbox Code Playgroud)
基准测试结果(计算机确实有2个核心):
user system total real
14.040000 0.030000 14.070000 ( 14.096566)
user system total real
13.970000 0.030000 14.000000 ( 14.023680)
Run Code Online (Sandbox Code Playgroud)
如您所见,使用线程,程序执行速度更慢.我假设线程正在被连贯地执行(由threads.each { |t| t.join }).有没有办法同时运行它们?或者以其他方式加速多核计算机上的ruby脚本执行?
我将不胜感激任何建议.
Ruby语言规范不保证线程实际上可以并行运行.绝大多数Ruby实现确实能够并行运行线程(例如Rubinius,JRuby,IronRuby,MacRuby,Ruby.NET),但MRI和YARV不能.
MRI使用绿色线程,它自己安排,但从不并行安排.YARV使用由操作系统调度的本机线程,但使用巨型VM锁(GVL)保护它们以防止它们并行调度.请注意,对于MRI和YARV,这仅适用于Ruby线程.MRI可以与其单个解释器线程并行运行多个C线程,例如,可以对I/O执行此操作; 此外,C扩展可以并行运行多个C线程.并且YARV仅阻止其线程进入VM,但是当它们在C库中或等待I/O或从C扩展运行某些C代码时,它们可以并行运行.
JRuby和IronRuby分别使用JVM线程和CLI线程.JRuby和IronRuby中没有任何东西阻止它们并行运行,但这取决于您正在使用的JVM或CLI实现.并非所有JVM都支持并行运行线程,尽管大多数主流都能够做到这一点.当然,您的操作系统也需要支持它,并且您需要实际拥有多个内核.
| 归档时间: |
|
| 查看次数: |
560 次 |
| 最近记录: |