PHP Microbenchmarking(变量与条件语句)

This question might be "a bit out there". But more or less it's microbenching code, but it's to improve my standards and general knowledge of PHP's backend.

SO! Here I go, my question! Would calling a variable twice require less memory (and load on CPU) than calling an additional else condition in PHP? Which requires more resources? And why?

The Below examples:
A, shows calling a variable twice and B, shows calling an additional else condition. Both have the same end result, of course.

Any additional references to any answers (responds) would be highly appropriated too! If possible.

Example A:

$a = 1;
if (isset($array['a']))
{
    $a = $array['a'];
}

$b = NULL;
if (isset($array['b']))
{
    $b = $array['b'];
}



Example B:

if (isset($array['a']))
{
    $a = $array['a'];
}
else
{
    $a = 1;
}

if (isset($array['b']))
{
    $b = $array['b'];
}
else
{
    $b = NULL;
}

I created a mini benchmark, to see which is faster. The following evaluates both functions exactly 100 times. Inside of the two functions, they evaluate your examples exactly 100,000 times. On my home Ubuntu web server, the output is similar to this.

6.0754749774933 = Giving the variable a default value.

4.8433840274811 = Using an else statement instead.

The second example (else statements) is two seconds faster, however the example is being executed 10,000,000 (10 million) times. In a real-life example, the readability of the code and what your team prefers is more important than saving a few milliseconds.

To answer your question, there is almost 0 difference when using either method.

If you want my opinion, I prefer the second example.

Here is the benchmarking code I used. http://phpfiddle.org/api/raw/8nm-d72

I haven't timed it, but there is no evaluation in the else. If you had an elseif there may be some infinitesimally small difference. This example probably comes down to readability and/or coding preference.

I have a feeling the PHP opcode generated by these two would be identical or equivalent for most current versions of php. Play with it yourself and see: http://blog.ircmaxell.com/2012/07/the-anatomy-of-equals-opcode-analysis.html

For fun, here's the opcode generated via php 5.3x for example A:

 2     0  >   ASSIGN                                                   !0, 1
 3     1      ZEND_ISSET_ISEMPTY_DIM_OBJ                    1  ~1      !1, 'a'
 4     2    > JMPZ                                                     ~1, ->6
 5     3  >   FETCH_DIM_R                                      $2      !1, 'a'
       4      ASSIGN                                                   !0, $2
 6     5    > JMP                                                      ->6
 8     6  >   ASSIGN                                                   !2, null
 9     7      ZEND_ISSET_ISEMPTY_DIM_OBJ                    1  ~5      !1, 'b'
10     8    > JMPZ                                                     ~5, ->12
11     9  >   FETCH_DIM_R                                      $6      !1, 'b'
      10      ASSIGN                                                   !2, $6
12    11    > JMP                                                      ->12
13    12  > > RETURN                                                   1

Here's example B:

 2     0  >   ZEND_ISSET_ISEMPTY_DIM_OBJ                    1  ~0      !0, 'a'
 3     1    > JMPZ                                                     ~0, ->5
 4     2  >   FETCH_DIM_R                                      $1      !0, 'a'
       3      ASSIGN                                                   !1, $1
 5     4    > JMP                                                      ->6
 8     5  >   ASSIGN                                                   !1, 1
11     6  >   ZEND_ISSET_ISEMPTY_DIM_OBJ                    1  ~4      !0, 'b'
12     7    > JMPZ                                                     ~4, ->11
13     8  >   FETCH_DIM_R                                      $5      !0, 'b'
       9      ASSIGN                                                   !2, $5
14    10    > JMP                                                      ->12
17    11  >   ASSIGN                                                   !2, null
19    12  > > RETURN                                                   1

You've got the same number of lines of code being processed, same number of assignments, and jumps. You're just jumping around it slightly different, but at quick glance it looks like the only difference is the order of execution, not the actual commands executed.

Disregarding the minimal performance difference, it would be a best practise to define a variable first.

PHP isnt type safe (yet) however, imagine your Example B results in 2 different types of variable $a.

if (isset($array['a']))
{
    $a = $array['a']; // could be a string or anything else
}
else
{
    $a = 1; // is an integer
}

For strict conditions in further code that could easily become an problem. Eg: assume $array['a'] has the value '1';

Following fould fail:

if ( $a === 1 ) // do something

Here a type and value has to match which would be TRUE only for the above else case $a = 1;