Which Is More Efficient?

closed account (zb0S216C)
I have these two lines of code:

Line 1:
X = ( XyzNew.X == 0 ) ? X : ( X /= XyzNew.X );

Line 2:
if( ( XyzNew.X <= -1 ) || ( XyNew.X >= 1 ) ) X /= XyzNew.X;

What I want to know is which of the previous lines of code is more efficient? I'm a perfectionist so I don't want buggy/messy code.

Wazzak
Last edited on
Uh... that question doesn't make that much sense. These 2 lines do different things.

Either way, optimize algorithms not statements.

PS: The ternary operator is hard to read, and if statements are too if you put everything in one line. Your compiler won't generate slower code if you use curly braces, but every else will have a harder time reading your code.
closed account (zb0S216C)
hanst99 wrote:
These 2 lines do different things. (sic)

That's why I asked to see which is more efficient.

hanst99 wrote:
The ternary operator is hard to read (sic)

Is it? I had to omit the documentation that was associated with that line for further clarity.

hanst99 wrote:
Your compiler won't generate slower code if you use curly braces (sic)

I know, but it's kinda pointless since it's only one statement.

hanst99 wrote:
every else will have a harder time reading your code. (sic)

Thanks for pointing that out but I must say that this code isn't the final implementation. When I do write the final product, I'll clarify the code as much as possible.

Thanks for your reply, though, Hanst :)

Wazzak


If you can achieve the same thing with a check for ==0 than you can with a check for <=-1 || >=1 - I don't know how that's possible, but I don't know what you're actually doing there either - then the statement that just performs a single check is, of course, faster.

But if your question was if
 
 a = condition ? b : c;


performs better than

1
2
3
4
5
6
7
if(condition)
{
    a =b;
} else 
{
    a = c;
}


- I seriously doubt that the instructions the compiler would generate from this would be different enough for it to make an impact on speed or memory usage. I'd actually guess that most compilers would generate the exact same instructions.


I just prefer it when people wrap even single instructions in curly braces - it's imho more readible (if statements already reduce readability of code enough as they are), also it allows you to quickly add additional stuff if it should ever turn out to be necessary. Also I just like uniform code formatting in general.

As to the readability of the ternary operator - I say this because it doesn't let you naturally read code.

1
2
3
a = b; //this is normally what you expect
a = condition ? a : b; /* leads you very often to read a = condition,
then when you see the ? you have to search the :, etc etc */
Last edited on
closed account (zb0S216C)
OK, that clears most of it up, but what about the assignment within the ternary operator? Allow me to elaborate. If XyzNew.X is equal to zero, X is assigned to itself. Since X already contains the value, will the assignment even take place?

Wazzak
What's with the unnecessarily complicated code? If both values are integers, this is what it should have been:
if (xyzNew.x!=0)x/=xyzNew.x;

Note that gcc generates the same code for all three variants.

Edit: oh, didn't see the latest replies.

If XyzNew.X is equal to zero, X is assigned to itself. Since X already contains the value, will the assignment even take place?

If it's a builtin or simple type, most likely not (assuming optimizations are turned on).
It doesn't matter though. This is more or less a misuse of the ternary operator.
Note that the assignment to itself always takes place, whether XyzNew.X equals zero or not.
Last edited on
These are poor attempts at being tricky.

Why would you want Line 1 to ever resolve to X = X? Just write your logic like you would speak it for clarity.

1
2
3
if( XyzNew.X != 0 ) {
    // do something
}


Generally speaking, I try to never use the ternary operator to make logic decisions. It is useful for quick in-place transformations of a value, though. Take console output for example:

1
2
bool b = true;
cout << "b == " << ( b ? "true" : "false" ) << endl;
closed account (zb0S216C)
Thanks for your input, lads. Well, the example above is only an example, so is the if statement. You've all gave me something to think about.

moorecm wrote:
These are poor attempts at being tricky. (sic)

I wasn't trying to be tricky. I was simply exploring possible options. I generally use the ternary operator in the same way you (Moorecm) stated. As I said, I was being curious.

Thanks again :)

Wazzak
Topic archived. No new replies allowed.