Reputation: 59323
At some point somebody suggested to me that the sluggishness in my PHP script could be partly because I was returning a large array from a function. Instead he suggested passing an object reference to the function and having the function working directly on the array.
If this makes a significant difference I'm planning on changing my function style to returning an array of information regarding the function execution (time benchmark, attempts etc.) and working directly on an object reference instead.
So what do you think?
Upvotes: 2
Views: 323
Reputation: 522015
No, it does not make any difference.
PHP implements copy-on-write, meaning that even if you assign a value to another variable or return it from a function, the value is simply passed by reference internally. It's only actually being copied when written to, and even then only if there are no other variables referencing it. Since you're returning the array from a function, no other variables should reference the array, hence there's no copying going on at any point.
PHP does a lot of optimization with regards to pointers, references and variable handling. Don't think you can outsmart it easily, PHP is too high-level for that. Write what you mean. And as always: profile to find the real bottlenecks.
Upvotes: 6