我有一些数组deep_array
深入哈希并需要时间访问,我还有一个变量(my_local_variable
)指向deep_array
和其他一些本地数组new_array
。我需要将deep_array
设置为new_array
到my_local_variable
。
相当于其中一项的东西:
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)
答案 0 :(得分:0)
告诉我,如果我错了,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 ------------------------------------------------------
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)
如果我使用数组索引
,差异会更大