Don't think, profile.
Run scripts that use each version of the function for 100,000 repetitions under, say, the Unix time
command. Do not philosophize about what is faster; find out.
Don't think, profile.
Run scripts that use each version of the function for 100,000 repetitions under, say, the Unix time
command. Do not philosophize about what is faster; find out.
[...] so little time optimizations count.
No, they don't.
The only true optimisation is the one that helps YOU read and/or write code faster. You should not sacrifice simplicity or readibility for performance - it will slow you down in long run.
Passing things by reference can be especially misleading. You may run into weird problems later, when some var will change mysteriously. You modify function input, which is not the way things normally work. Every time you do thing the unusual way you have to remember about them. Your memory and attention is limited though. Computer's is not. Don't overoptimise.
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. --Brian W. Kernighan
UPDATE
In this case your whole function escapeCSV
is slightly pointless. You should use fputcsv
, which is core PHP library written in C, thus is by far faster and memory efficient.
My guess would be that this is a hardly noticeable optimization, that could potentially lead to unexpected sideeffects down the road. For example, the next guy is probably NOT going to expect escapeCSV
to modify his parameter, and he might use the parameter he passed in thinking it is still the un-CSV'd string.
Also, since strings aren't edited in-place, you've already got two cases where a new string is created in your function (one for str_replace, one for the concatenation of quotes before/after it). One more for the pass-by-value case isn't going to be the bottleneck in your app.
PHP references are not pointers. They do not speed anything up - In fact they do the contrary, since they require an additional internal entry in the symbol table.