Page 1 of 1
Strings: best practice?
Posted: Tue Feb 06, 2007 4:50 pm
by aos24
I know that it's possible to update a character of a string in place -- e.g. $message[4] = 'T' -- but is this considered best practice? Are PHP strings mutable? (If not, would the interpreter create a separate copy of the entire $message string when the above code is executed?)
Thanks.
Posted: Tue Feb 06, 2007 4:59 pm
by Christopher
Strings are variables so yes. Whether you use substr() or $str[] does not matter -- they are for you to store string data and manipulate however you like.
Posted: Tue Feb 06, 2007 5:17 pm
by aos24
Thanks. I don't think you can assign to substr() -- I get "Can't use function return value in write context" when I try it.
So would, say, updating several hundred characters in a 1-4K string using $message[$i] = $newchar be reasonably efficient? (I.e., it wouldn't create hundreds of copies of $message for the garbage collector to deal with?)
Posted: Tue Feb 06, 2007 5:43 pm
by feyd
Do some testing.
Posted: Tue Feb 06, 2007 6:00 pm
by aos24
Thanks, but how am I supposed to reliably determine what the garbage collector is doing?
Posted: Tue Feb 06, 2007 6:13 pm
by feyd
Well, the most reliable way is to look at the internal code that would be run.. but since that's a rats' nest... Profilers may offer information, as may using crude checks in PHP.
Posted: Tue Feb 06, 2007 6:38 pm
by aos24
I could do that, but I was hoping someone here would just know, and would be able to say either, "Yes, updating strings in place is considered good practice", or, "Don't do that: it will work but you'll create lots of temporary strings for the garbage collector to clean up!"
Posted: Tue Feb 06, 2007 6:42 pm
by Christopher
aos24 wrote:Thanks, but how am I supposed to reliably determine what the garbage collector is doing?
In general you should not be concerned with "what the garbage collector is doing", only that it is doing it. If you have such extreme memory management issues then you would probably build a C extension to PHP to deal with it or use a language where you manage you own memory allocation.
Though I honestly can't imagine that you actually need to be concerned about memory management. Plus the time and cost of doing so would cost be far, far more than a few bucks for more memory.
Posted: Tue Feb 06, 2007 7:03 pm
by aos24
I really don't want to get down and dirty with the garbage collector! What I want to do is to establish good coding practices for strings, so that if there are several comparable approaches to a programming task I can choose the one that will wreak the least havoc under the covers. Once I've done that, I'll be happy to code away without worrying too much about memory management.
Posted: Tue Feb 06, 2007 7:17 pm
by Christopher
The problem with trying to "wreak the least havoc under the covers" in a language like PHP is that how thing work under the covers can and does change from release to release. So unless you have some very critical memory issue and are going to do thorough testing each release you use then it is just not worth it. PHP has an extensive library of
string functions for string manipulation -- I would recommend seeing if there is something there that does what you need first.
Posted: Wed Feb 07, 2007 8:22 am
by volka
Code: Select all
<?php
$start = memory_get_usage();
$f = str_repeat('-', 65536);
echo '#1 ', memory_get_usage() - $start, "\n";
$a = $f;
echo '#2 ', memory_get_usage() - $start, "\n";
$a = str_replace('-', '+', $f);
echo '#3 ', memory_get_usage() - $start, "\n";
$b = $f;
echo '#4 ', memory_get_usage() - $start, "\n";
$b[4] = 'x';
echo '#5 ', memory_get_usage() - $start, "\n";
?>
#1 65796
#2 65844
#3 131456
#4 131504
#5 197052
Adding a simple new reference to the string does not increase the memory usage much. But changing the string does. The "old" unchanged value is still accessible via $f. str_repeat and $b[4]= create a new string. String are immutable in php.
Posted: Wed Feb 07, 2007 2:06 pm
by RobertGonzalez
It looks like
str_replace() and the string position changes use about the same amount of memory. Or am I reading that wrong?
Posted: Wed Feb 07, 2007 3:59 pm
by aos24
Thanks, volka, that was very helpful. Great FAQ, too.