I am expanding and improving the Java application , which also performs long searches using a small DSL (it is used in detail for Model-Discovery, but it is generally NP-Complete).
During this search, I want to show a small progress bar on the console. Due to the general structure of DSL, I cannot calculate the total size of the search space. Therefore, I can only infer the course of the first backtracking statement.
Now the question is: I can use the flag for each backtracking statement to indicate that this statement should report progress. When evaluating the statement, I can check the flag using the if statement:
public class EvalStatement { boolean reportProgress; public EvalStatement(boolean report) { reportProgress = report; } public void evaluate() { int progress = 0; while(someCondition) {
I can also use two different classes:
- Class that does nothing
- Subclass that concludes
It will 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) {
Now really the question (s):
- Is searching in Java a method faster than calling an if statement?
- Also, will the interface and two independent classes be faster?
I know that Log4J recommends putting an if-statement around log calls, but I think the main reason is to build parameters, espacially lines. I have only primitive types.
EDIT : I clarified the code a bit (which is often called ... using a singleton doesn't matter here).
In addition, I did two long-term searches of the search, where the if statement, respectively, was called the operation call 1.840.306.311 times on the machine, doing nothing:
if version took 10h 6min 13sek (50.343 hits per second)- Version
or took 10h 9min 15sek (50.595 hits per second)
I would say this does not give a real answer, because the difference of 0.5% is in measurement.
My conclusion: they more or less behave the same, but the basic approach can be faster in the long run, as Kane suggested in the answers.