I have a peace of precedural code which is executed in a loop. This peace of code requires one large array, but only for reading and not manipulating purposes. I generate this array once before the loop.
Now I would like to move this code into a function in order to use it again is some other script, but I don’t know how to that in a clean way, but still having a good performance:
- Generating this large array every time in the function itself would be bad, since it is always the same array and creates unnecessary overhead.
- I dont want to use a global array, since this is bad practise
- I guess therefore it would be the best to pass it as a paremeter every time I call the function.
I don’t like that last option too much either, because it clutters the argument list and I am not sure about the overhead caused through this. As I understand it the array would be copied every time – would it be a good idea to pass it by reference therefore (by using the & operator)?
One, who is really concerned in performance, should
avoid processing large arrays in a performance-critical code.
instead of playing tricks of speeding up an essentially slow process.
So, without knowing a REAL CASE (the purpose, processing and content of the array) the only answer could be:
Thus, turn your array into a database and select only necessary data.
This is a general answer to such a general question like this.