0

In PHP is it considered good practise to store things like cookie or session information inside variables instead of callling them over and over. For example:

$happiness_level = $_SESSION['happiness_level'];

echo $happiness_level.' something';
echo $happiness_level.' something else';

vs:

echo $_SESSION['happiness_level'].' something';
echo $_SESSION['happiness_level'].' something else';

Is the 2nd one worse for performance. I ask because in jQuery it's good performance practice to cache selectors by assigning them to variables so the DOM doesn't have to be traversed each time the selector is called.

I was wondering if a similar rule applies in PHP. Or does PHP recognize internally that it already grabbed the session variable named "happiness_level" so the 2nd time it's called, instead of doing all the extra work of looking up it's value again, it instead just uses the original value from the 1st call?

Basically which is better (example 1 or 2) for performance, even if we're talking milliseconds?

Adam Wagner
  • 15,469
  • 7
  • 52
  • 66
TKpop
  • 309
  • 1
  • 4
  • 8
  • Something so small as this, you're talking picoseconds. – Niet the Dark Absol Dec 31 '11 at 04:52
  • Some very crude benchmarking points to about 60 nano seconds on my computer (though I have medium-high end hardware). – Corbin Dec 31 '11 at 05:14
  • 1. adds nothing, so don't do it. –  Dec 31 '11 at 05:52
  • It adds readability. That tends to be important to me. $happiness_level is a shorter to digest than $_SESSION['happiness_level']. (Though if it really was used in only two lines and right below the assignment, then it is kind of pointless) – Corbin Dec 31 '11 at 07:21
  • Second one consumes less memory. So eventually it's better from a performance stand point http://code.google.com/speed/articles/optimizing-php.html – mixdev Jan 22 '12 at 20:39

4 Answers4

3

When he has explicity tagged a question micro-optimisation, I don't think he will find a bunch of "don't worry about it!" answers very helpful. (Though it is true that this question is very, very negligible in terms of performance optimisation.)

Anyway, I suspected assigned to a variable would be quicker. If PHP did some kind of sneaky code optimisations, it might be able to be optimized out, it does not look like that's what is done (after some very, very, very crude benchmarking).

$array['key'] requires looking up a value in a hash table, whereas $val is just a normal variable. The overhead of looking it up in a hash table causes a very slight performance penalty since the key must be hashed, and so on and so forth.

(Note: that last paragraph completely neglects how variables in general are stored. My point is that arrays in this context contain a hash table within the variable, whereas the other one is a variable that is psuedo-directly accessed [in PHP land it is directly accessed anyway].)

Corbin
  • 33,060
  • 6
  • 68
  • 78
1

It is not the thing you should ever think of. Focus on application design

Basically which is better (example 1 or 2) for performance, even if we're talking milliseconds?

Milliseconds hunting makes no sense in your case - just write the code you can effectively work with

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • there's more than just performance here, 1 adds nothing. may as well take one step back for every million forward, insignificant, but still pointless. –  Dec 31 '11 at 05:53
1

Performance is not a problem here. Code readability is.

echo $happiness_level.' something';
echo $happiness_level.' something else';

is clearly more readable than

echo $_SESSION['happiness_level'].' something';
echo $_SESSION['happiness_level'].' something else';

If you're not worried about how good your code looks, you're free to use either one of the options.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
0

I would try to benchmark it by running a loop around both versions. Have a timer start before running each loop and stop it when the loop is done. Whicherver has the shortest time is better for performance.

My bet is on them both being just about the same.

phirschybar
  • 8,357
  • 12
  • 50
  • 66