1

I am reviewing a colleague's code and have noticed something that I think is inefficient and wasteful.

Basically he has a function like this:

function blah($record) {
    echo "do something " . $record['first_name'] . $record['last_name'];
}

He is passing an entire array to this function without about 30 odd records.

Inside the function he only ever uses $record['first_name'] and $record['last_name'] so all the other attributes are pointless.

My argument is. Wouldn't it be better to rewrite it like this:

function blah($first_name, $last_name) {
    echo "Do something " . $first_name . $last_name;
}

And then simply use the parameters instead of accessing the array during the function.

Is this an accurate observation? Will memory be shallow copied by php unnecessarily in this instance? Or am I being over pedantic.

4
  • 2
    To get a definitive answer you'll need to profile the code. Expect to find that it makes little or no difference. This sort of micro-optimisation is rarely useful and often wastes more time in coding and testing than you will ever recover in improved performance. Commented Jan 6, 2014 at 7:03
  • Wouldn't that mean everything is edited by reference then? Commented Jan 6, 2014 at 7:04
  • 2
    This is just a personal opinion, but (unless the function is called like 9999999 times) the improvement would be around a few milliseconds. Also the function may be using some more of that array sometime in the future, and passing 2 variables, then adding one more parameter may become a pain. So... yeah, it may be an improvement but I'd go with over pedantic in this particular case. Commented Jan 6, 2014 at 7:05
  • Cool, yes optimisations are important in the code base. And it is unlikely to ever change past first_name and last_name. Commented Jan 6, 2014 at 7:07

1 Answer 1

1

Nothing will be copied in memory just by passing an array into a function. There's no performance loss anywhere. It may still be a good idea to rewrite the function signature to make it more explicit, but that's debatable and cannot be judged without more context.

Sign up to request clarification or add additional context in comments.

6 Comments

If no memory is copied anywhere, wouldn't that mean all php function parameters are passed by reference? Surely there must be a memcopy at some point.
PHP uses copy-on-write. Data is only copied when you modify it. As long as you're only reading from a value it doesn't need to be copied and isn't.
Ahh ok, so it will do the copying when he calls $record['first_name']
So from this I assume the only optimisation is some tiny reference that php must keep to all the other keys in the array incase it has to copy-on-write the data?
Basically, PHP will remember that it will have to duplicate the array structure (note: not even necessarily the data within it) if the array is ever written to.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.