• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Ternary operator v/s if condn

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I want to know which of the two is more efficient.
The ternary operator or the if condition.
dont mind the readablity and easily understandable
parameters.
I just want to know which one is faster at any given time.
thankz,
Chinmay.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly: don't ignore readability! (pray that the person writing the code that you will one day maintain believes in readable code!)
Here's the (hardly conclusive) test I ran. The strange values are to attempt to reduce the ability of compilers to optimise code...

The results I got were 1252 and 1061 ms.
After 100 million iterations.
So yes, there does appear to be approximately 20% better performance for the ternary operator, but unless you're calling it multiple millions of times, its unlikely to make any difference to your code and the ternary operator is evil.
Strong words? I'm not so sure. I used to use it fairly often and wrote some completely unreadable code using it. I'm not sure anyone will thank you for using it, specially when there is no perceivable advantage.
Dave
[ April 09, 2002: Message edited by: David O'Meara ]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without denying that you can write unreadable code using the ternary operator, isn'tclearer and more immediately expressive thanGranted, many developers will need to look twice at the first option because they're not used to seeing the ternary operator. But that's hardly an argument, is it? -- avoiding the ternary operator because developers aren't used to seeing it because the ternary operator tends to be avoided because developers aren't used to seeing it because...
Avoid nested ternaries like the plague tho'...
- Peter
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter den Haan:
Avoid nested ternaries like the plague tho'...


Why? Just because you "aren't used to seeing it"...
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, Dirk - please play nice .
(Ignore the fact that I am laughing please).
 
Peter den Haan
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dirk Schreckmann:
Why? Just because you "aren't used to seeing it"...

Oh believe me, Dirk, I'm very much used to seeing it alright and worse...
- Peter
 
Sheriff
Posts: 17687
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter den Haan:
Avoid nested ternaries like the plague tho'...


I recently read an article in JavaWorld that had some nested ternaries. I thought the formatting helped a lot in making it more readable although I still did a double take on it. I agree that you should generally avoid them.
I think it was something along the lines of
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Peter.
I think a Java programmer should know what does the ternary operator mean.
If not, he should find the meaning, and then, using it, because it�s cleaner and smaller.
This way, we�ll make grow every Java programmer knowlegde! (kidding)
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

I was wondering how ternary and if / else operators would behave with Java 1.6.

I took the small test class mentioned at the beginning of this thread, played around with different numbers ... and my measures showed that runtimes are identical.

My suggestion: For better readability and lower maintenance costs: Avoid inline conditionals (ternary operator).

My conclusion: Modern compilers produce the same binary code independent what operator you use in the source file.

Cheers, Martin
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's see.

Have a look at this program:


Now, let's compile it and then look at the byte code.

javac Example.java
javap -c Example


Result:
Compiled from "Example.java"
public class Example extends java.lang.Object{
public Example();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return

public static void main(java.lang.String[]);
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: bipush 13
5: invokestatic #3; //Method method1:(I)Ljava/lang/String;
8: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
11: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
14: bipush 13
16: invokestatic #5; //Method method2:(I)Ljava/lang/String;
19: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
22: return

public static java.lang.String method1(int);
Code:
0: iload_0
1: bipush 10
3: if_icmpge 11
6: ldc #6; //String Less than 10
8: goto 13
11: ldc #7; //String Not less than 10
13: areturn

public static java.lang.String method2(int);
Code:
0: iload_0
1: bipush 10
3: if_icmpge 9
6: ldc #6; //String Less than 10
8: areturn
9: ldc #7; //String Not less than 10
11: areturn

}


Note that the byte code of the two methods is almost exactly the same. There's a small difference, in that the version with the ternary operator contains a goto instruction, while the version with the if just immediately does areturn. Which of these is faster or more efficient is hard to say.

In practice you will not notice any difference between using a ternary operator or using an if statement.

Do not use one or the other just because you think one is more efficient than the other. These kind of micro-optimisations almost never make any sense.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And if they make sense then it's certainly not "Beginning Java". This topic has changed into something that is more suited in Java in General. So I'm moving it.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic