Storing comparisons

I have a program which computes a number, stores it and uses it for many times.
Then it proceeds to the next iteration (it’s a loop), generates another number, stores it and uses it as well for many times.

Considering I have to store a lot of values, i will store them in an array called value_holder

Is it the same thing, computationally, doing:

for i = 1:x
    value_holder[i] = function()
    stuff that uses: value_holder[i]
end

and:

for i = 1:x
    value_name = function()
    value_holder[i] = value_name
    stuff that uses: value_name
end

No I think the former will likely read from memory every time you access value_holder[i]. That read likely is quite cheap as the value will be cached so it won’t make a huge difference.

There will be a big difference if you had multiple threads writing to value_holder even if they don’t directly overwrite results due to “false sharing”.

1 Like