Optimizing code


The code example is in Processing but the question is about general optimization. I’m also interested in resources that discuss optimization if anyone has suggestions.

The specific question is whether there is a difference between these two approaches:

  1. Single, complex conditional: if ((this or this) and (this or this)) {}
      if (((objektVf.x >= other.objektV.x && objektVf.x <= other.objektV2.x) 
        (objektV2f.x >= other.objektV.x && objektV2f.x <= other.objektV2.x))
      ((objektVf.y >= other.objektV.y && objektVf.y <= other.objektV2.y) 
      (objektV2f.y >= other.objektV.y && objektV2f.y <= other.objektV2.y))) {
          objektS.x *= -1;
          objektS.y *= -1;
  1. Nested conditional: if (this or this) { if (this or this) {} }
      if ((objektVf.x >= other.objektV.x && objektVf.x <= other.objektV2.x) || (objektV2f.x >= other.objektV.x && objektV2f.x <= other.objektV2.x)) {
         if ((objektVf.y >= other.objektV.y && objektVf.y <= other.objektV2.y) || (objektV2f.y >= other.objektV.y && objektV2f.y <= other.objektV2.y)) {
          objektS.x *= -1;
          objektS.y *= -1;

What I don’t know is whether in #1, the process stops after the first condition is not true. If it does not, it seems like #2 is more optimal.


When it comes to optimizing if / then statements, the most optimal way is the way that fails the fastest.

So, when you are considering how to order and group things, make your best guess as to what will be most likely to fail and test that first.

Then, keeping in mind order of operations (including parenthesis, etc), construct if statements that will fail as quickly as possible so that all of the following statements won’t even have to be evaluated.

Finally (and perhaps most importantly in your case above) – if you ever see and && or an || that includes equals signs in both comparisons … something can probably be optimized.

The two examples above are equivalent in most cases and will be combined by the compiler. Use whichever one is more readable and easier to debug (my personal vote is for 1 because you’re less likely to lose the 2nd if).

Here’s my simplified version for my brain … before I mess with it … what exactly are you trying to do?

float x0 = objektVf.x;
float x1 = other.objektV.x;
float x2 = other.objektV2.x;
float x3 = objektV2f.x;

float y0 = objektVf.y;
float y1 = other.objektV.y;
float y2 = other.objektV2.y;
float y3 = objektV2f.y;

 if (((x0 >= x1 && x0 <= x2) || (x3 >= x1 && x3 <= x2)) && 
     ((y0 >= y0 && y0 <= y2) || (y3 >= y0 && y3 <= y2))) 
    objektS.x *= -1;
    objektS.y *= -1;

Whenever I see that many >= and <= I start to wonder if things could be optimized …


And to add to @bakercp, I was taught that && and || were short-circuit evaluating in many languages (including Java). So for your: if ((A or B) and (C or D)) {}, if A and B are false, then the compiler knows the statement will be false and hence won’t even check C or D.


what exactly are you trying to do?

I am just going through simple collision detection, again. It’s something I return to at least a couple times a year. This time I wrote it from scratch using a class and PVectors. Both of the examples above work equally well but I am trying to stretch into new things. I was thinking about optimization more after I talked with someone about modulo being a three step operation on my laptop but a single step on my tablet.

I watched a video on GJK earlier. I am not ready for that yet. :smile:


Speaking of “what exactly are you trying to do?” (a really good question, I’m not complaining) we should have an ATS talk or something sometime about coding as practice and pleasure. I would really like my students to take up coding in the same way artists take up drawing as a regular practice of pleasure through practice of something challenging and rewarding without a goal. Had anyone written on that? It seems like all the code initiatives are about getting things done, usually for employability or STEM.


I code for pleasure. :smile:

Depending on your collision needs (I’m assuming 2d) you might cache an axis aligned bounding box (AABB) for each colliding object (you can calculate an AABB for any 2d shape and update it as it rotates, etc … if it’s a collection of points, just collect the maxx, maxy, minx, miny) and check the AABBs for intersections (it’s a super fast check of overlapping rectangles) … if you google AABB collision testing, you’ll find loads and loads and loads of stuff.

You’ll then quickly start hearing about broad phase sweeping, space partitioning, etc. The goals of all of these algorithms is to reduce the collision “search” space. Ideally you should only test for collisions if the two objects have any reasonable chance of being close together. So, sometimes people will store their colliding objects in data structures (like kdtrees, or Barnes-Hut, etc) so that they are only tested for collisions with the ones that are nearby.

… can of worms opened. :slight_smile:


One thing to think about for optimization is how often that piece of code is run. The modulo one is important because it could happen millions of time over the course of a applications lifetime so the time saved through optimization is significant. If the code is only run once, unless it is extremely poorly written doesn’t need to be scrutinized all that much.

That being said processing will run slower because it has to run on a virtual machine, its java afterall so thats always something to consider, though most computers today are far more optimized with their resources so the performance difference isn’t like night and day.

Your question from what I can gather is what is more optimized a single if statement with a bunch of conditionals or a nested if statement. Thing is it probably is more likely to be the single line but really that depends on the compiler and how it handles everything. Like @bakercp and @mikewesthad mentioned short-circuit evaluation is most likely the more efficient route to go.


I code for pleasure.

I assumed that! :smiley:


I totally agreed and hope we have some workshops/classes like that. :smile: