Reputation: 63
Setting up a temporary variable to swap two elements in array seems to be more efficient than using parallel assignment. Can someone help explaining?
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
Results in:
user system total real
4.470000 0.020000 4.490000 ( 4.510368)
user system total real
3.220000 0.010000 3.230000 ( 3.255109)
Upvotes: 6
Views: 238
Reputation: 36101
The parallel assignment creates a temporary array, which it afterwards splats.
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
The one extra Array
comes from ObjectSpace.each_object(Array).count
itself.
Another way to verify - look at the instructions:
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
Upvotes: 5