Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Process.fork会使Ruby on OS X中的东西变慢?_Ruby_Macos_Performance_Unix_Fork - Fatal编程技术网

为什么Process.fork会使Ruby on OS X中的东西变慢?

为什么Process.fork会使Ruby on OS X中的东西变慢?,ruby,macos,performance,unix,fork,Ruby,Macos,Performance,Unix,Fork,有人能给我解释一下为什么在Ruby中,Process.fork会让东西慢很多吗?我正在OS X El Capitan上使用Ruby 2.3.1 require 'time' require 'benchmark' def do_stuff 50000.times { Time.parse(Time.utc(2016).iso8601) } end puts Benchmark.measure { do_stuff } # => 1.660000 0.010000 1.670

有人能给我解释一下为什么在Ruby中,
Process.fork
会让东西慢很多吗?我正在OS X El Capitan上使用Ruby 2.3.1

require 'time'
require 'benchmark'

def do_stuff
  50000.times { Time.parse(Time.utc(2016).iso8601) }
end

puts Benchmark.measure { do_stuff } # => 1.660000   0.010000   1.670000 (  1.675466)

Process.fork do
  puts Benchmark.measure { do_stuff } # => 3.170000   6.250000   9.420000 (  9.508235)
end
编辑:刚刚注意到在Linux(测试过的Debian或Ubuntu)上运行该代码不会对性能产生负面影响。

为什么Process.fork会使Ruby on OS X中的东西变慢?

弄清这一点的第一步是减少变量的数量

您运行
Time.parse(Time.utc(2016).iso8601)
50000次的示例似乎异常具体。我使用不同的“慢”Ruby任务重新制定了基准测试:

require 'benchmark'

def do_stuff
  a = [nil] * 200

  10.times do
    a.each {|x| a.each {|y| a.each {|z| ; }}}; ()
  end
end

puts "main: #{Benchmark.measure { do_stuff }}"

Process.fork do
  puts "fork: #{Benchmark.measure { do_stuff }}"
end
在这里,我将您的
Time
命令替换为大型数组上的无操作嵌套循环

结果是:

main:   4.020000   0.010000   4.030000 (  4.050664)
fork:   3.940000   0.000000   3.940000 (  3.962207)

main:   3.840000   0.010000   3.850000 (  3.856188)
fork:   3.850000   0.000000   3.850000 (  3.865250)

main:   3.930000   0.000000   3.930000 (  3.937741)
fork:   3.970000   0.000000   3.970000 (  3.986397)

main:   4.340000   0.010000   4.350000 (  4.370009)
fork:   4.300000   0.000000   4.300000 (  4.308156)
没有明显的分叉过程慢或快的模式。我已经在OSX和Ubuntu上用Ruby 1.9、2.0和2.3进行了测试,测试结果保持不变

你的问题的答案是:

Process.fork
通常不会使Ruby on OS X中的东西变慢。

然而,这里有一个不同的有趣问题,那就是