Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8037965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:04:49+00:00 2026-06-05T03:04:49+00:00

I have a pathfinding algorithm which is run many times and must be extremely

  • 0

I have a pathfinding algorithm which is run many times and must be extremely efficient, so I’m wondering what I can do to boost performance. I have an if statement which says this:

if (!(n != 1 && map.isCornerObstructed(p)) {
    // Do stuff...
}

It seems to me that the double inversion would take slightly longer than this logically equivalent version:

if (n == 1 || !map.isCornerObstructed(p)) {
    // Do stuff...
}

The problem is that the former is more readable in the context of the code, so I’m somewhat reluctant to change it if I don’t know what the result will be.

Is one more efficient than the other? Or is the Java compiler smart enough to optimize things like this automatically?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T03:04:50+00:00Added an answer on June 5, 2026 at 3:04 am

    The code

    Set<String> set = new HashSet<String>();
    int n = set.size();
    Object o = new Object();
    if (!(n != 1 && set.contains(o))) {
      System.out.println("Foo");
      // Do stuff...
    }
    

    generates bytecode

     0  new java.util.HashSet [16]
     3  dup
     4  invokespecial java.util.HashSet() [18]
     7  astore_1 [set]
     8  aload_1 [set]
     9  invokeinterface java.util.Set.size() : int [19] [nargs: 1]
    14  istore_2 [n]
    15  new java.lang.Object [3]
    18  dup
    19  invokespecial java.lang.Object() [8]
    22  astore_3 [o]
    23  iload_2 [n]
    24  iconst_1
    25  if_icmpeq 38
    28  aload_1 [set]
    29  aload_3 [o]
    30  invokeinterface java.util.Set.contains(java.lang.Object) : boolean [25] [nargs: 2]
    35  ifne 46
    38  getstatic java.lang.System.out : java.io.PrintStream [29]
    41  ldc <String "Foo"> [35]
    43  invokevirtual java.io.PrintStream.println(java.lang.String) : void [37]
    46  return
    

    The code

    Set<String> set = new HashSet<String>();
    int n = set.size();
    Object o = new Object();
    if (n == 1 || !set.contains(o)) {
      System.out.println("Foo");
      // Do stuff...
    }
    

    generates bytecode

     0  new java.util.HashSet [16]
     3  dup
     4  invokespecial java.util.HashSet() [18]
     7  astore_1 [set]
     8  aload_1 [set]
     9  invokeinterface java.util.Set.size() : int [19] [nargs: 1]
    14  istore_2 [n]
    15  new java.lang.Object [3]
    18  dup
    19  invokespecial java.lang.Object() [8]
    22  astore_3 [o]
    23  iload_2 [n]
    24  iconst_1
    25  if_icmpeq 38
    28  aload_1 [set]
    29  aload_3 [o]
    30  invokeinterface java.util.Set.contains(java.lang.Object) : boolean [25] [nargs: 2]
    35  ifne 46
    38  getstatic java.lang.System.out : java.io.PrintStream [29]
    41  ldc <String "Foo"> [35]
    43  invokevirtual java.io.PrintStream.println(java.lang.String) : void [37]
    46  return
    

    which is exactly the same. Therefore, there will be no performance difference at all, no matter how finely you measure it. The compiled code is identical.

    (Note that the reason for this is that javac actually goes and decomposes the if statement into individual condition tests and branches, so it actually works out the pathway for each possibility.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have a simple pathfinding algorithm which precalculates the shortest route to several
I seem to have a error in the following A* pathfinding implementation, which I
Im wondering if I can optimize my pathfinding code a bit, lets look at
I have a tail recursive pathfinding algorithm that I've implemented in JavaScript and would
Have one Doubt In MVC Architecture we can able to pass data from Controller
Have a simple one-off tasks which needs a progress bar. OpenSSL has a useful
I'm facing a path finding problem in which I have to find every possible
I've made a pathfinding visualizer using python and pygame. As of now, it can
Let's say I have a 2d grid. Pathfinding is a secondary step in my
Have advancements in CPU design like dynamic instruction scheduling narrowed the performance gap between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.