Ruby 中的并行分配性能

parallel assignment performance in Ruby

设置一个临时变量来交换数组中的两个元素似乎比使用并行赋值更有效。谁能帮忙解释一下?

require "benchmark"

Benchmark.bm do |b|
  b.report do
    40000000.times { array[1], array[2] = array[2], array[1] }
  end
end

Benchmark.bm do |b|
  b.report do
    40000000.times do
      t        = array[1]
      array[1] = array[2]
      array[2] = t
    end
  end
end

结果:

   user     system      total        real
   4.470000   0.020000   4.490000 (  4.510368)
   user     system      total        real
   3.220000   0.010000   3.230000 (  3.255109)

并行赋值会创建一个临时数组,然后展开。

GC.disable

def with_temp
  a = 1
  b = 2

  t = a
  a = b
  b = t
end

def with_parallel
  a = 1
  b = 2

  a, b = b, a
end

before_all = ObjectSpace.each_object(Array).count
with_temp
after_with_temp = ObjectSpace.each_object(Array).count
with_parallel
after_with_parallel = ObjectSpace.each_object(Array).count

GC.enable

puts after_with_temp - before_all          # => 1
puts after_with_parallel - after_with_temp # => 2

额外的 Array 来自 ObjectSpace.each_object(Array).count 本身。


另一种验证方式-查看说明:

puts RubyVM::InstructionSequence.compile("a = 1; b = 2; t = a; a = b; b = t").disasm
puts RubyVM::InstructionSequence.compile("a = 1; b = 2; a, b = b, a").disasm

== disasm: @>==========
local table (size: 4, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 4] a [ 3] b [ 2] t
0000 trace 1 ( 1)
0002 putobject_OP_INT2FIX_O_1_C_
0003 setlocal_OP__WC__0 4
0005 putobject 2
0007 setlocal_OP__WC__0 3
0009 getlocal_OP__WC__0 4
0011 setlocal_OP__WC__0 2
0013 getlocal_OP__WC__0 3
0015 setlocal_OP__WC__0 4
0017 getlocal_OP__WC__0 2
0019 dup
0020 setlocal_OP__WC__0 3
0022 leave


== disasm: @>==========
local table (size: 3, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 3] a [ 2] b
0000 trace 1 ( 1)
0002 putobject_OP_INT2FIX_O_1_C_
0003 setlocal_OP__WC__0 3
0005 putobject 2
0007 setlocal_OP__WC__0 2
0009 getlocal_OP__WC__0 2
0011 getlocal_OP__WC__0 3
0013 newarray 2
0015 dup
0016 expandarray 2, 0
0019 setlocal_OP__WC__0 3
0021 setlocal_OP__WC__0 2
0023 leave