Arrays ruby通过指向原始数组的另一个数组设置数组
我有一些数组Arrays ruby通过指向原始数组的另一个数组设置数组,arrays,ruby,performance,optimization,elementwise-operations,Arrays,Ruby,Performance,Optimization,Elementwise Operations,我有一些数组deep\u array在散列中很深,需要时间才能访问,我还有一个变量(my\u local\u variable)指向deep\u array,还有一些其他的局部数组new\u array。我需要通过my\u local\u variable将deep\u array设置为new\u array 相当于以下其中一个的东西: my_local_variable.map!.with_index {|_, i| new_array[i]} my_local_variable.each_
deep\u array
在散列中很深,需要时间才能访问,我还有一个变量(my\u local\u variable
)指向deep\u array
,还有一些其他的局部数组new\u array
。我需要通过my\u local\u variable
将deep\u array
设置为new\u array
相当于以下其中一个的东西:
my_local_variable.map!.with_index {|_, i| new_array[i]}
my_local_variable.each_with_index {|_, i| b[i] = new_array[i]}
但要快得多
编辑:速度
这是我正在处理的情况的大致情况:
(事实上,我写得更深,但我写得更少)
给出:
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)
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)
如果我错了,请告诉我,
Array#replace
似乎比当前的任何答案都快,因为它不需要对新数组进行.dup
复制,也不需要创建其他引用:
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 ---------------------------------------------------
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)
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)
如果我用数组编制索引,差异会更大,告诉我我是否错了,但是
Array#replace
似乎比当前的任何答案都快,因为它不需要对新数组进行.dup
复制,也不需要创建另一个引用:
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 ---------------------------------------------------
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)
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\u array
设置为new\u array
(即deep\u array.object\u id==new\u array.object\u id
,或者只需要它们具有相同的内容?在引用deep\u array的哈希中,您可以使用新的数组引用来切换它,这样就不必复制了。{key=>deep_array}您可以将其更改为{key=>new_array},但不确定这是否是您要查找的内容。@muistooshort no,new array
在这种情况下是一个缓存值,因此它应该保持原样is@Ucpuzz是的,但是deep_数组的位置更像hash[a][b][c][d]
和a,b,c和d本身就是数组,必须进行散列,访问深度数组
太慢,这就是为什么我有一个本地引用如果你有一个比你现有的引用高一级的引用,那么可能无论什么[:你的数组]=新的数组.dup
都可以工作,不过,我不知道这是否会比逐个元素手动复制要快得多。如果您有如此复杂且嵌套深入的数据,那么您可能应该重新考虑如何管理数据。deep\u array
是否需要设置为new\u array
(即deep\u array.object\u id==new\u array.object\u id
)还是只需要它们具有相同的内容?在引用任何深度数组的哈希中,您可以使用新的数组引用来切换,这样就不必复制。{key=>deep_array}您可以将其更改为{key=>new_array},但不确定这是否是您要查找的内容。@muistooshort no,new array
在这种情况下是一个缓存值,因此它应该保持原样is@Ucpuzz是的,但是deep_数组的位置更像hash[a][b][c][d]
和a,b,c和d本身就是数组,必须进行散列,访问深度数组
太慢,这就是为什么我有一个本地引用如果你有一个比你现有的引用高一级的引用,那么可能无论什么[:你的数组]=新的数组.dup
都可以工作,不过,我不知道这是否会比逐个元素手动复制要快得多。如果您有如此复杂且嵌套深入的数据,那么您可能应该重新考虑如何管理数据。