Why does Process.fork make things slower in Ruby on OS X?

Can someone explain to me why Process.fork makes stuff in Ruby much slower? I am using Ruby 2.3.1 on OS X El Capitan.

 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 

EDIT :. I just noticed that running this code on Linux (Debian testing or Ubuntu) does not negatively impact performance.

+4
source share
1 answer

"Why does Process.fork make things slower in Ruby on OS X?"

Step one is to minimize the number of variables.

Your example of running Time.parse(Time.utc(2016).iso8601) fifty thousand times seems strange. I reformulated a test test using another β€œslow” Ruby task:

 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 

Here I replaced your Time commands with a no-op nested loop with a large array.

Results:

 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) 

The lack of a noticeable bifurcated process structure is slower or faster. I tested in Ruby 1.9, 2.0, and 2.3 on both OS X and Ubuntu, and it remains the same.

The answer to your question:

Process.fork , in general, doesn't make things slower in Ruby on OS X.

However, there is another interesting question: Why is `Time.utc` slower in a forked process in Ruby on OS X (and not Python)?

+1
source

Source: https://habr.com/ru/post/1261976/


All Articles