Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of Overriding vs. if-statement

I'm extending and improving a Java application which also does long running searches with a small DSL (in detail it is used for Model-Finding, yes it's in general NP-Complete).

During this search I want to show a small progress bar on the console. Because of the generic structure of the DSL I cannot calculate the overall search space size. Therefore I can only output the progress of the first "backtracking" statement.

Now the question: I can use a flag for each backtracking statement to indicate that this statement should report the progress. When evaluating the statement I can check the flag with an if-statement:

public class EvalStatement {
  boolean reportProgress;

  public EvalStatement(boolean report) {
     reportProgress = report;
  }

  public void evaluate() {
    int progress = 0;

    while(someCondition) {
      // do something

      // maybe call other statement (tree structure)

      if (reportProgress) {
         // This is only executed by the root node, i. e.,
         // the condition is only true for about 30 times whereas
         // it is false millions or billions of times
         ++progress;
         reportProgress(progress);
      }
    }
  }
}

I can also use two different classes:

  • A class which does nothing
  • A subclass that is doing the output

This would look like this:

public class EvalStatement {
  private ProgressWriter out;
  public EvalStatement(boolean report) {
      if (report)
        out = new ProgressWriterOut();
      else
        out = ProgressWriter.instance;     
  }
  public void evaluate() {
    while(someCondition) {
      // do something
      // maybe call other statement (tree structure)
      out.reportProgress(progress);
    }
  }
}

public class ProgressWriter {
  public static ProgressWriter instance = new ProgressWriter();
  public void reportProgress(int progress) {}
}

public class ProgressWriterOut extends ProgressWriter {
  int progress = 0;
  public void reportProgress(int progress) {
    // This is only executed by the root node, i. e.,
    // the condition is only true for about 30 times whereas
    // it is false millions or billions of times
    ++progress;
    // Put progress anywhere, e. g., 
    System.out.print('#');
  }
}

An now really the question(s):

  • Is the Java lookup of the method to call faster then the if statement?
  • In addition, would an interface and two independet classes be faster?

I know Log4J recommends to put an if-statement around log-calls, but I think the main reason is the construction of the parameters, espacially strings. I have only primitive types.

EDIT: I clarified the code a little bit (what is called often... the usage of the singleton is irrelevant here).

Further, I made two long-term runs of the search where the if-statement respectively the operation call was hit 1.840.306.311 times on a machine doing nothing else:

  • The if version took 10h 6min 13sek (50.343 "hits" per second)
  • The or version took 10h 9min 15sek (50.595 "hits" per second)

I would say, this does not give a real answer, because the 0,5% difference is in the measuring tolerance.

My conclusion: They more or less behave the same, but the overriding approach could be faster in the long-term as guessed by Kane in the answers.

like image 977
H-Man2 Avatar asked Oct 31 '11 18:10

H-Man2


People also ask

What is the advantage of using override?

Advantages of Method Overriding Method overriding helps in writing a generic code based on the parent class. It provides multiple implementations of the same method and can be used to invoke parent class overridden methods using super keyword. It defines what behavior a class can have.

Does if else affect performance?

If-else statements optimization Based on these conditions, we determine some lines of code to execute. Unfortunately, using too many if-else conditional statements can affect performance because the Java virtual machine will have to compare the conditions.

What are the conditions for overriding?

Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited, then it cannot be overridden.

Can we change the visibility of method while overriding?

The class implementing the interface cannot change the visibility of the method (we cannot change it from public to protected).


2 Answers

I think this is the text book definition of over-optimization. You're not really even sure you have a performance problem. Unless you're making MILLIONS of calls across that section it won't even show up in your hotspot reports if you profiled it. If statements, and methods calls are on the order of nanoseconds to execute. So in order for a difference between them you are talking about saving 1-10ns at the most. For that to even be perceived by a human as being slow it needs to be in the order of 100 milliseconds, and that's if they user is even paying attention like actively clicking, etc. If they're watching a progress bar they aren't even going to notice it.

Say we wanted to see if that added even 1s extra time, and you found one of those could save 10 ns (it's probably like a savings of 1-4ns). So that would mean you'd need that section to be called 100,000,000 times in order to save 1s. And I can guarantee you if you have 100 Million calls being made you'll find 10 other areas that are more expensive than the choice of if or polymorphism there. Seems sorta silly to debate the merits of 10ns on the off chance you might save 1s doesn't it?

I'd be more concerned about your usage of a singleton than performance.

like image 156
chubbsondubs Avatar answered Oct 06 '22 03:10

chubbsondubs


I wouldn't worry about this - the cost is very small, output to the screen or computation would be much slower.

like image 26
Roman Goyenko Avatar answered Oct 06 '22 03:10

Roman Goyenko