1

I always look into how to optimize the code more and more every day, and how I can learn to code faster code.

Looking at some minified JS online I notice the change of a few IF into a statement and I thought that it might be a very good idea to start using it in PHP as well.

<?php
if(!isset($myVar) || $myVar == 'val'){
    $myVar = 'oldVal';
}
if(isset($myVar2) && $myVar2 == 'oldVal'){
    $myVar2 = 'newVal';
}
?>

into

<?php
(!isset($myVar) || $myVar == 'val') && $myVar = 'oldVal';
isset($myVar2) && $myVar2 == 'oldVal' && $myVar2 = 'newVal';
?>

As I like the new syntax, I started to use it more and more thinking to save processing time, but do I really save any or there is no difference internally between the two ?

(The example code is just an EXAMPLE, to only show the technique)

Fabrizio
  • 3,734
  • 2
  • 29
  • 32
  • The question title contradicts with question body. You cannot make your code run faster with micro-optimizations. – Your Common Sense Sep 12 '13 at 16:13
  • have you profiled your code? – hek2mgl Sep 12 '13 at 16:13
  • 3
    As a general rule that I follow, it's not worth it to micro-optimize like this if the code becomes hard to read. I couldn't tell immediately what the "after" did, but I understood the "before" code right off the bat. You might want to take a look at [this question](http://stackoverflow.com/q/4236533/899126), which also talks about micro-optimization in PHP. – Chris Forrence Sep 12 '13 at 16:15
  • 5
    This _might_ be quicker. But is it readable? **Hell no.** This is confusing. Don't do this. **Maintainability** is the word... The aim of minification is to reduce downloaded "boilerplate" content minimal... Also, there was this _random happy chap_ who said **"Premature optimization is the root of all evil"** for a reason... – ppeterka Sep 12 '13 at 16:16
  • Totally agree with @ChrisForrence – Pascamel Sep 12 '13 at 16:16
  • Your inline statements are horrendous, and unnecessary. You should re-evaluate your logic. Your first if block should assign `'newVal'` instead of `'oldVal'` because otherwise, it will just be reassigned to `'oldVal'` in the second if block anyways. Thus, you can speed things up by skipping that check. Furthermore, you don't need to check if `$myVar` is set in the second if block. **It will always be set, because you set it if it is not set in the first if block.** I deleted my answer below because I feel like you might have just made a poor example to illustrate what you were really asking. – crush Sep 12 '13 at 16:27
  • please keep in mind that the example code is just that.. an EXAMPLE. I know that the code is useless as it is. My point was direct on the "What does PHP do internally different ?" I agree that readability is important, but I do not have any problems reading either of the statements, initially I did, now I am at the point that I can see them both and recognize what happen right away – Fabrizio Sep 12 '13 at 16:31
  • See it like this: `connect(db_info) && die($error)` . Isn't this the same as `$var=connect(db_info) && $var1='connected'` ? (again, examples) – Fabrizio Apr 04 '14 at 15:16

1 Answers1

2

I used this code to profile both approaches:

<?php

$iterations = 1000000;
$startTime = microtime( true );

$i = 0;
while( ++$i < $iterations ) {
    if(!isset($myVar) || $myVar == 'val'){
        $myVar = 'oldVal';
    }
    if(isset($myVar) && $myVar == 'oldVal'){
        $myVar = 'newVal';
    }
}

echo 'First Running Time: ' . (microtime(true) - $startTime) . "\n";


$startTime = microtime( true );
$i = 0;
while( ++$i < $iterations ) {
    (!isset($myVar) || $myVar == 'val') && $myVar = 'oldVal';
    isset($myVar) && $myVar == 'oldVal' && $myVar = 'newVal';
}

echo 'Second Running Time: ' . (microtime(true) - $startTime) . "\n";

The results:

(1st Run)

First Running Time: 0.38401508331299

Second Running Time: 0.40315389633179

(2nd Run)

First Running Time: 0.38593697547913

Second Running Time: 0.40187788009644

Conclusion: Your method is slower, but the amount is so small that even if it weren't you would still be better off writing more readable code.

Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • Well.. testing like this is known to be kind of skewed by possibly memory or CPU usage of the machine at the time etc... My question was more direct to the "What does PHP do internally ?" – Fabrizio Sep 12 '13 at 16:29