Default variable vs else condition

#1

The code above and the below are same in result.
I like to think which one is better in performance.

(1) The code1 is better in performance because there is no else condition .
(2) The code2 is better in performance because there is no default variable settting.

I guess (1) the code1 is better.

What is your think?

#2

The first code example is using two different target variable names :grin:

Have you seen the ternary operator?

I think this is what it should look like:

$targetVariable = ($condition===$yes) ? 0 : 1;

If the $condition is true or false

$targetVariable = $condition ? 0 : 1;

Off topic - has the format as code button been removed from the editor?

#3

Cool, it works fine.
Thank you, Rubble.

I am afraid that I don’t understand your question.
where is the code button?

I, now, got it.  thank you again.
#4
**code3**

$targetVar1='a';
$targetVar2='b';

if($condition===$yes) { 
$targetVariable1='c';
$targetVariable1='d'; 
}

I have the code1 above and code2 below.
And there has same result.

**code4**

if($condition===$yes) { 
$targetVariable1='c';
$targetVariable3='d'; 
} else {
$targetVar1='a';
$targetVar2='b';
}

I am wondering which is bettter in performance.

I bet the code3 is better in performance.

the would-be below, doesn’t work correctly

would-be code
$targetVariable1, targetVariable2 = ($condition===$yes) ? 'a','b' : 'c','d';
#5

Sorry the part about the code button was for the admin as I could not find it for my reply

Is it the preformatted text :thinking: I use it so irregularly now I forget.

#6
**code3**

$targetVar1='a';
$targetVar2='b';

if($condition===$yes) { 
$targetVariable1='c';
$targetVariable1='d'; 
}

I have the code1 above and code2 below.
And they has same result.

**code4**

if($condition===$yes) { 
$targetVariable1='c';
$targetVariable3='d'; 
} else {
$targetVar1='a';
$targetVar2='b';
}

I am wondering which is bettter in performance.

I bet the code3 is better in performance.

The would-be code below, doesn’t work correctly

Which one is better in performance betwwen code3 and code4
OR, do you have any better code instead of the would-be code below which causes php error?

would-be code
$targetVariable1, targetVariable2 = ($condition===$yes) ? 'a','b' : 'c','d';
#7

I have an idea, how about telling us what the actual problem is you are trying to solve with this code.

#8

The difference in performance between the bits of code is no negligible I really wouldn’t worry about it.

Code wise I like example without the else better as they pose less of a cognitive strain to me.

Also see object calistenics - although I don’t necessarily agree with all of them you should check out at least the part about Don’t use the else keyword.

Never say never, but a lot of times solutions without else are nicer and easier to read that solutions with else.

#9

It’s the </> button, 6th from the left.

#10

It’s not valid code, that’s why. You can’t just stick a second variable in each spot and hope the language will deal with it. If you separated it out into two separate expressions, one for each target, it would work as you want it to.

As for the performance thing, it is negligible as @rpkamp said, for the example code. Obviously if the question is because you’re doing this a million times, then your negligible performance differences will mount up. Which is why @benanamen asked about the context.

Your code isn’t clear, though, because as @Rubble said earlier, the variables created inside the if structure are not the same as the default variables you created before it. If they are not intended to be, then the main difference between code2 and code1 is that, if you use $targetVariable later in the code, you have a chance in code2 that it will not have been defined at all.

Apart from the first line, where you set $defaultvar, that is:

code2

$defaultvar=1;
 if($condition===$yes) {
 $targetVariable=0;
 } else {
 $targetVariable=1;
 }

Do you have a development environment set up? Then stick both bits of code in a loop to execute them a few hundred thousand times, and compare the results. Then you won’t have to bet which is better, you’ll have tangible test data to show you.

They don’t do the same thing as each other, though, so it’s a bit difficult to compare - you’re comparing different jobs rather than different methods of coding the same job.

You could increase the performance of code3 by not assigning a value to the same variable twice, just set it to ‘d’ in the first place.

**code3**

$targetVar1='a';
$targetVar2='b';

if($condition===$yes) { 
$targetVariable1='c';
$targetVariable1='d'; 
}
#11

When you do this, you need to take care of the distribution of $condition as well. If it’s a 50/50 coin toss the results will be different than for 10/90 or 90/10.

At the end of day, all of this is constant time (O(1) in big oh notation) so unless you need to do this millions of times within a few seconds you’re better off focussing your performance efforts on stuff that matters, like SQL queries.

#12

Well technically you could do this…

list($foo, $bar) = $condition ? ['a', 'b'] : ['c', 'd'];

… but as you seem to be concerned with micro optimisations, creating temporary arrays is probably not that good an idea here. ^^

closed #13

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.