Riesling
Riesling

Reputation: 6593

How to make a large array usable within a function with good performance

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

Answers (4)

Kescha Skywalker
Kescha Skywalker

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

Falcon
Falcon

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

Your Common Sense
Your Common Sense

Reputation: 157981

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.

Upvotes: 3

DaneSoul
DaneSoul

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

Related Questions