0

Problem details. I need to create a framework to perform various checks, like: - is Date A between dates B and C? - is Integer A greater than Integer B and smaller than Integer C? etc. So far, i am thinking of two possible implementations, detailed bellow.

Impl1 - using a single class to perform the checks, based on the check type.

import java.sql.Time;
import java.util.Date;

public class SearchManager {

    public final static int SEARCH_TYPE_DATE = 0;
    public final static int SEARCH_TYPE_INT = 1;
    public final static int SEARCH_TYPE_STRING = 2;
    public final static int SEARCH_TYPE_TIME = 3;

    private final int searchType;

    public SearchManager(int searchType) {
        this.searchType = searchType;
    }

    public final boolean doCompare(Object minValue, Object maxValue, Object toBeCompared) {
        switch (this.searchType) {
            case SEARCH_TYPE_DATE: {
                return compareDates((Date) minValue, (Date) maxValue, (Date) toBeCompared);
            }
            case SEARCH_TYPE_INT: {
                return compareIntegers((Integer) minValue, (Integer) maxValue, (Integer) toBeCompared);
            }
            case SEARCH_TYPE_STRING: {
                return compareStrings(String.valueOf(minValue), String.valueOf(maxValue), String.valueOf(toBeCompared));
            }
            case SEARCH_TYPE_TIME: {
                return compareTimes((Time) minValue, (Time) maxValue, (Time) toBeCompared);
            }
            default:
                return false;
        }
    }

    private boolean compareDates(Date min, Date max, Date toBeCompared) {
        boolean result = false;
        // actual comparison
        return result;
    }

    private boolean compareIntegers(Integer min, Integer max, Integer toBeCompared) {
        boolean result = false;
        // actual comparison
        return result;
    }

    private boolean compareStrings(String min, String max, String toBeCompared) {
        boolean result = false;
        // actual comparison
        return result;
    }

    private boolean compareTimes(Time min, Time max, Time toBeComparedDate) {
        boolean result = false;
        // actual comparison
        return result;
    }
}

Impl2 - Using an abstract class or interface, and having an implementation of the comparison method for each search type.

public abstract class AbstractSearch {

public final static int SEARCH_TYPE_DATE = 0;
public final static int SEARCH_TYPE_INT = 1;
public final static int SEARCH_TYPE_STRING = 2;
public final static int SEARCH_TYPE_TIME = 3;

public AbstractSearch() {
    super(); //just for fun
}

protected abstract boolean doCompare(Object minValue, Object maxValue, Object toBeComparedValue);

}

Now, in this example, for X different search types, as you can imagine, X implementations of the AbstractSearch will be created.

Just imagine yourself that the class AbstractSearch from the 2nd implementation will need to perform additional tasks, other than the method doCompare(..) and that is why an interface is not my 1st candidate for this solution, and to write something like

public abstract class AbstractSearch implements Searcheable

would not help me a lot, since AbstractSearch or SearchManager will handle ALL the comparisons, and, if a new comparison type should be needed, an additional type/subclass implementation will be declared for corresponding super classes from Impl1 or Impl2.

My question is about which implementation is faster? And this is very important, since the comparison process will be called in loops containing thousands of elements. Thank you for reading/answering my question.

EDIT1: Also, please have in mind the fact that minValue and maxValue will be extracted from the classes that extends the AbstractSearch, for the second example, or classes extending SearchManager, as for the 1st example. These implementation will actually be graphical components allowing the user to enter a minimum and a maximum value, and then, these value will be compared in a loop with some bean property, of objects displayed in a table.

EDIT2: I am doing some benchmarks, with dummy implementations (i just want to compare the method call time vs switch execution time). The results are..surprising:

  • Using AbstractSearch (500k loops): -0.047 seconds
  • Using SearchManager (500k loops): -0.422 seconds

Having these results, it is safe to assume that using inheritance is much faster than using a switch (or even worse an if-else test) ?

1
  • It's not surprising that AbstractManager wins... calling an inherited method just requires fetching a function pointer from a method table and calling it, whereas the switch requires multiple compares and branches (which can result in pipeline stalls when the CPU can't correctly guess which way the branch will go).
    – Alex D
    Feb 24, 2012 at 22:56

4 Answers 4

1

If you want to make this code as fast as possible, also try using overloaded methods like this:

public final static boolean doCompare(Date min, Date max, Date toCompare) {
  // ...
}
public final static boolean doCompare(int min, int max, int toCompare) {
  // ...
}
// ...and so on

At compile time, the compiler will generate a direct call to the appropriate method, based on the types which you pass. (If you are passing Object references which might point to an instance of any of the 4 types, this won't work.)

If the values which you are comparing are ints, passing them to a method which takes Object arguments will require boxing and unboxing, which adds overhead.

If performance is really important, I recommend you use static methods, since they are a bit faster in many Java implementations.

Also, rather than using compareTo, you can probably squeeze out a bit more performance by using your own inline code for the comparisons.

EDIT: You said in the edited question that min and max will actually be passed in by a subclass of SearchManager. In that case I would make SearchManager abstract, and put different implementations of doCompare in each subclass of SearchManager. What I said about static methods won't work in this case.

1
  • That's exactly what i was thinking, but i needed to hear another opinion. For your patience and comment, i am awarding the best answer choice to you. Congrats and thanks.
    – hypercube
    Feb 24, 2012 at 23:30
1

I would think that the best idea would be to combine both ideas (and make it more type safe) and take advantage of the fact that all the types that you provide in your example (Date, Time, String, and Integer) are Comparable

public final <A> boolean doCompare (Comparable<A> min, Comparable<A> max, A target)
{
   return (min.compareTo(target) < 0) && (max.compareTo(target) > 0)
}

This is definitely a lot faster than the first implementation since it does not have to do any type checks (all checks will be done compile time) and is type safer and no slower than the second (while also being more flexible about type).

0

Is this just something that you can do with a comparison? (It looks like it.) Is this method essentially the same for all three implementations?

If so, just write

static <T extends Comparable<T>> boolean doCompare(T min, T max, T toCompare) {
  // impl here
}
0

Fun question! Why don't you benchmark your two implementations?

1
  • I am doing that as we speak. But i don't think i have all the answers. Perhaps someone here comes with a totally different implementation, that is superior to any of my scenarios.
    – hypercube
    Feb 24, 2012 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.