The Question of By Reference Recommended on Arrays?
This is something I needed to clear up awhile back. In PHP 5.3+, I wanted to ask if this improves performance on a very large array result? And do you have a way I can demonstrate the proof?
$synonyms = & MobyThesaurus::GetSynonyms("check");
Note the ampersand (by referen开发者_Python百科ce, instead of by value).
PHP uses copy-on-write behind the scenes. Meaning, values are only copied when they're altered. Until then there's no copying going on and $synonyms
basically acts as a reference anyway. If you're only ever reading from the array, there should be no difference in performance.
Once you are writing to the array, it does make quite a bit of difference in functionality whether the variable is a reference or not. Don't use references unless you mean to, or you may introduce funky side effects into your app.
There are a many optimizations going on behind the scenes, don't expect to be able to optimize it any more with such "tricks". PHP is the wrong language for being clever with pointer/reference acrobatics. :-)
精彩评论