ruby 设置一个数组通过另一个数组指向原来的
ruby set an array through another array pointing to the original
我有一些数组 deep_array
位于哈希深处并且需要时间来访问,我还有一个变量 (my_local_variable
) 指向 deep_array
和其他一些本地数组 new_array
。我需要通过 my_local_variable
.
将 deep_array
设置为 new_array
相当于其中之一的东西:
my_local_variable.map!.with_index {|_, i| new_array[i]}
my_local_variable.each_with_index {|_, i| b[i] = new_array[i]}
但更快
编辑:速度
这是对我正在处理的情况的粗略了解:
(实际上它更深,但我写的更少)
require 'benchmark'
H = {[1,2,3]=>[2,3,4],[3,4,5]=>[4,5,6],[5,6,7]=>[6,7,8]}
h = H[[1,2,3]]
Benchmark.bmbm(15) do |i|
i.report('local reference') {1_000_000.times {|i| h[0] = i}}
i.report(' index') {1_000_000.times {H[[1,2,3]][0] = i}}
end
给出:
Rehearsal ---------------------------------------------------
local reference 0.230000 0.010000 0.240000 ( 0.234168)
index 5.780000 0.040000 5.820000 ( 5.851909)
------------------------------------------ total: 6.060000sec
user system total real
local reference 0.220000 0.000000 0.220000 ( 0.226742)
index 5.770000 0.030000 5.800000 ( 5.830011)
如果我错了请告诉我,但是 Array#replace
似乎比当前的任何答案都快,因为它不需要 .dup
licated 新数组并且不需要其他参考待创建:
new_arrays = 1000000.times.map {4.times.map {rand 10}}
require 'benchmark'
Benchmark.bmbm(18) do |i|
i.report(' find, replace') do
hash = {0=>{1=>{2=>{3=>[]}}}}
1000000.times do |j|
hash[0][1][2][3].replace new_arrays[j]
end
end
i.report(' find, dup') do
hash = {0=>{1=>{2=>{3=>[]}}}}
1000000.times do |j|
hash[0][1][2][3] = new_arrays[j].dup
end
end
i.report('reference, replace') do
hash = {0=>{1=>{2=>{3=>[]}}}}
reference = hash[0][1][2][3]
1000000.times do |j|
reference.replace new_arrays[j]
end
end
i.report(' above ref, dup') do
hash = {0=>{1=>{2=>{3=>[]}}}}
above = hash[0][1][2]
1000000.times do |j|
above[3] = new_arrays[j].dup
end
end
end
给出:
Rehearsal ------------------------------------------------------
find, replace 1.670000 0.090000 1.760000 ( 1.791100)
find, dup 2.010000 0.110000 2.120000 ( 2.193884)
reference, replace 0.460000 0.000000 0.460000 ( 0.470288)
above ref, dup 1.390000 0.020000 1.410000 ( 1.421624)
--------------------------------------------- total: 5.750000sec
user system total real
find, replace 0.960000 0.020000 0.980000 ( 1.039140)
find, dup 1.720000 0.010000 1.730000 ( 1.753176)
reference, replace 0.470000 0.000000 0.470000 ( 0.472988)
above ref, dup 1.360000 0.020000 1.380000 ( 1.384833)
如果我用数组索引,差异会更大
我有一些数组 deep_array
位于哈希深处并且需要时间来访问,我还有一个变量 (my_local_variable
) 指向 deep_array
和其他一些本地数组 new_array
。我需要通过 my_local_variable
.
deep_array
设置为 new_array
相当于其中之一的东西:
my_local_variable.map!.with_index {|_, i| new_array[i]}
my_local_variable.each_with_index {|_, i| b[i] = new_array[i]}
但更快
编辑:速度
这是对我正在处理的情况的粗略了解:
(实际上它更深,但我写的更少)
require 'benchmark'
H = {[1,2,3]=>[2,3,4],[3,4,5]=>[4,5,6],[5,6,7]=>[6,7,8]}
h = H[[1,2,3]]
Benchmark.bmbm(15) do |i|
i.report('local reference') {1_000_000.times {|i| h[0] = i}}
i.report(' index') {1_000_000.times {H[[1,2,3]][0] = i}}
end
给出:
Rehearsal ---------------------------------------------------
local reference 0.230000 0.010000 0.240000 ( 0.234168)
index 5.780000 0.040000 5.820000 ( 5.851909)
------------------------------------------ total: 6.060000sec
user system total real
local reference 0.220000 0.000000 0.220000 ( 0.226742)
index 5.770000 0.030000 5.800000 ( 5.830011)
如果我错了请告诉我,但是 Array#replace
似乎比当前的任何答案都快,因为它不需要 .dup
licated 新数组并且不需要其他参考待创建:
new_arrays = 1000000.times.map {4.times.map {rand 10}}
require 'benchmark'
Benchmark.bmbm(18) do |i|
i.report(' find, replace') do
hash = {0=>{1=>{2=>{3=>[]}}}}
1000000.times do |j|
hash[0][1][2][3].replace new_arrays[j]
end
end
i.report(' find, dup') do
hash = {0=>{1=>{2=>{3=>[]}}}}
1000000.times do |j|
hash[0][1][2][3] = new_arrays[j].dup
end
end
i.report('reference, replace') do
hash = {0=>{1=>{2=>{3=>[]}}}}
reference = hash[0][1][2][3]
1000000.times do |j|
reference.replace new_arrays[j]
end
end
i.report(' above ref, dup') do
hash = {0=>{1=>{2=>{3=>[]}}}}
above = hash[0][1][2]
1000000.times do |j|
above[3] = new_arrays[j].dup
end
end
end
给出:
Rehearsal ------------------------------------------------------
find, replace 1.670000 0.090000 1.760000 ( 1.791100)
find, dup 2.010000 0.110000 2.120000 ( 2.193884)
reference, replace 0.460000 0.000000 0.460000 ( 0.470288)
above ref, dup 1.390000 0.020000 1.410000 ( 1.421624)
--------------------------------------------- total: 5.750000sec
user system total real
find, replace 0.960000 0.020000 0.980000 ( 1.039140)
find, dup 1.720000 0.010000 1.730000 ( 1.753176)
reference, replace 0.470000 0.000000 0.470000 ( 0.472988)
above ref, dup 1.360000 0.020000 1.380000 ( 1.384833)
如果我用数组索引,差异会更大