Results 1 to 5 of 5
  1. #1
    kimhonoridez is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default Effect of Multiple Condtitions in if() statements to run time

    I would like to ask the effect of multiple conditions in if() statements especially to the runtime of the program.

    Example:

    if (!((this.currentPage instanceof Churvaness)
    && (this.currentPage instanceof Eklavush)
    && (this.currentPage instanceof Skimberlu)
    && (this.currentPage instanceof Wakekek)
    && (this.currentPage instanceof Piskot)
    && (this.currentPage instanceof Seikaku)
    && (this.currentPage instanceof Pakbuak)
    && (this.currentPage instanceof Ugubanpa))) {

    I am calling this one when a transition from one page to another occurs.

    Please help me.

    Thanks!

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    well, each component in the if statement is evaluated from left to right. and the evaluation stops when it encounters a false condition.

    so in the best performance case, the first piece,
    Java Code:
    (this.currentPage instanceof Churvaness)
    , if that was false in this case, then I would expect the rest of the && ... things to not be evaluated. .though because you have the entire bunch of these enclosed in () and negated ,the resulting if operation would return true.

    the worst case performance would be when all of the components evaluated to true.. now in this example, i'm not sure exactly how bad that is, comparing an object reference to a class type is probably pretty fast anyway.

    one thing to consider for this example, instead of negating an AND, you could do an or of a negated value (you know, Demorgan's theorim)

    not (A * B *... * X) == not(A) + not(B) + ... + not(X)

    so, this could be expressed as
    Java Code:
    if (!(this.currentPage instanceof Churvaness)
    || !(this.currentPage instanceof Eklavush)
    || !(this.currentPage instanceof Skimberlu)
    || !(this.currentPage instanceof Wakekek)
    || !(this.currentPage instanceof Piskot)
    || !(this.currentPage instanceof Seikaku)
    || !(this.currentPage instanceof Pakbuak)
    || !(this.currentPage instanceof Ugubanpa)) {
    Which is clearer to see that when the object is not an instance of the given test class will return true, and in an or expression, I believe it should stop considering the components when it discovers the first true result, as this would satisfy the "OR" requirement.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Quite apart from the efficiency (or not) of that code, if your logic is conditional on the type of a variable, you have a design flaw. Instead of that, retain state that directs the program flow.

    db

  4. #4
    kimhonoridez is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    thank you so much for the info.

    It really helped me internalize my design...

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by kimhonoridez View Post
    if (!((this.currentPage instanceof Churvaness)
    && (this.currentPage instanceof Eklavush)
    && (this.currentPage instanceof Skimberlu)
    && (this.currentPage instanceof Wakekek)
    && (this.currentPage instanceof Piskot)
    && (this.currentPage instanceof Seikaku)
    && (this.currentPage instanceof Pakbuak)
    && (this.currentPage instanceof Ugubanpa))) {
    This conditional expression (and Travishein's version) made me shiver. I take it that those right hand operands are interfaces because the entire expression wouldn't make sense if they were classes. What object implements all those interfaces (so the if-condition would be false)? It boggles me ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. problem with htttpSession with multiple browsers at a time
    By sandeepsai39 in forum New To Java
    Replies: 1
    Last Post: 11-01-2010, 11:48 AM
  2. Waiting on multiple semaphores at the same time
    By flok in forum Threads and Synchronization
    Replies: 3
    Last Post: 11-10-2009, 03:31 PM
  3. upload multiple picture at one time
    By chaiwei in forum Java Applets
    Replies: 3
    Last Post: 10-30-2009, 05:39 AM
  4. Make multiple JTextFields focus at the same time.
    By toymachiner62 in forum AWT / Swing
    Replies: 6
    Last Post: 10-09-2009, 02:59 AM
  5. Smoke Effect help
    By jammyjamsticy in forum AWT / Swing
    Replies: 1
    Last Post: 04-20-2009, 12:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •