your first example we are chewing up memory creating a copy of $counter
Correct.
in the second example we are pointing to the original. The implication being that example two is more efficient/faster/leet? Is that right.
Correct as well. The example I used does not really offer any performance benefits because it is too small.
Lets supposed you have a million element array. In the first example you would, create 2 copies of that million element array in your program(So it takes more memory). You would also be passing million values to and from the function(so it would be slower).
If you use a reference you have one copy of that array. You only passing a single value to the function. This can be provide enormous performances benefits when working with a large amount of data.
There are times you want to make a copy because you don't want to change the original data. But if you are modifying the original values you should use references.
|