Reputation: 6593
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:
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)?
Upvotes: 1
Views: 230
Reputation: 489
Use OO-Programming, create a class with two methods, one for generating array and the second for the calculation using the array. Use patterns like Singleton to secure, that your object is the only instance or use static variables. Make a package to share this Class between different scripts/projects.
Since the question is very general is the answer to very abstract.
Upvotes: 1
Reputation: 259
I guess passing it by refrence is good enough but sometimes generate time call pass by refrence warning.. Might cause you trouble later.
Upvotes: -2
Reputation: 157981
One, who is really concerned in performance, should
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:
This is a general answer to such a general question like this.
Upvotes: 3
Reputation: 4511
You can serialize your array and write serialized in file. So it would be more fast to read that file, unserialize and use it later in your code.
Upvotes: 1