Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This question already has an answer here:

I have heard that generally, an expression like:

if (true === $variable)

is faster than:

if ($variable === true)

My question is about the performance, not the readability. Questions such as this one do not mention explicitly in any of their answers whether there is a performance difference between the two.

Is it true? Is the first form really faster than the second?

share|improve this question

marked as duplicate by gnat, GlenH7, MichaelT, MainMa, Robert Harvey Apr 21 at 15:33

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

10  
Oh dear, the worst kind of performance myth: Zero justification, doesn't align with any reasonable model of computation, nano optimization even if it has a grain of truth, and doesn't even drive misguided coders to better (in a non-performance sense) code. Care to share where you've heard that? –  delnan Apr 21 at 7:10
    
A colleague of mine said he read it somewhere. I googled, but couldn't find the source, so I asked here. –  Parham Doustdar Apr 21 at 7:22
    
@gnat: My question is about the performance advantage, rather than the possibility of assignment in an if statement. –  Parham Doustdar Apr 21 at 7:25
6  
answers in duplicate question, as well as in another similar question, cover this: reasons for Yoda condition have nothing to do with performance –  gnat Apr 21 at 7:27
    
@delnan Oh it's very obvious where he heard that from and there's actually a good reason to do so. if (foo = true) is an awful bug to track down, while if (true = foo) just won't compile. Today with static analyzers that will point this out immediately less of a deal and it always looked horrible to me, but there's some reason for it. –  Voo Apr 21 at 19:13

2 Answers 2

up vote 12 down vote accepted

If there is a performance difference (which I highly doubt) it will be extremely tiny.

This is a micro-optimization which is too frivolous to worry about. Every programmer at one point or another goes through a stage where he believes streamlining his usage of basic language features will make his program faster. It won't.

Rather than finding ways to make your life difficult for no reason (micro-optimizing at high levels is one of them), instead focus on creating valuable products and pleasing your customers.

share|improve this answer
1  
You said what I exactly have been trying to tell my colleague (and failing). I'd give you 5 upvotes if I could :-D I guess I need to be more subtle when telling him? –  Parham Doustdar Apr 21 at 11:26
    
@Parham: Ditto. It's a case of the streetlight effect. –  Mike Dunlavey Apr 22 at 13:57

Well, why you are not testing it then?

$var = false;

$start = microtime(1);
for ( $i = 0; $i < 10000; $i++ )
    if ( true === $var ) {}
$end = microtime(1);
$first = $end - $start;

$start = microtime(1);
for ( $i = 0; $i < 10000; $i++ )
    if ( $var === true ) {}
$end = microtime(1);
$second = $end - $start;

if ( $first > $second ) {
    $diff = $first - $second;
    echo 'constant === variable finished ' . $diff . ' faster';
} else {
    $diff = $second - $first;
    echo 'variable === constant finished ' . $diff . ' faster';
}

At least in my computer the result is quite variable, and doesn't totally function like what your friend were suggesting.

P.S. I usually write in the true === $var style, however that's a personal preference not a an optimization trick, etc.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.