I have some array deep_array
that's deep in a hash and takes time to access, I also have a variable (my_local_variable
) pointing to deep_array
and some other local array new_array
. I need to set deep_array
to new_array
through my_local_variable
.
Something equivalent to one of these:
my_local_variable.map!.with_index {|_, i| new_array[i]}
my_local_variable.each_with_index {|_, i| b[i] = new_array[i]}
but much faster
Edit: speed
This is a rough idea of the situation I'm dealing with:
(in reality it's deeper but i'm doing fewer writes)
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
Gives:
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)