Apache CollectionUtils Performance

1.3k Views Asked by At

I was wandering if anyone made a benchmark on Apache CollectionUtils. In my simple benchmark:

List<Integer> ints = Arrays.asList(3, 4, 6, 7,8, 0,9,2, 5, 2,1, 35,11, 44, 5,1 ,2);
    long start = System.nanoTime();
    ArrayList<Integer> filtered = new ArrayList<Integer>(ints.size());
    for (Integer anInt : ints) {
        if (anInt > 10) {
            filtered.add(anInt);
        }
    }
    long end = System.nanoTime();
    System.out.println(filtered + " (" + (end - start) + ")");

    Predicate<Integer> predicate = new Predicate<Integer>() {
        @Override
        public boolean evaluate(Integer integer) {
            return integer > 10;
        }
    };
    start = System.nanoTime();
    filtered.clear();
    CollectionUtils.select(ints, predicate,filtered);
    end = System.nanoTime();
    System.out.println(filtered + " (" + (end - start) + ")");

I got the following results:

[35, 11, 44] (127643)
[35, 11, 44] (3060230)

I must say Im a big fan of this library coz it makes the code clean and testable but currently Im working on performance sensetive project and Im afraid my affection to this library gonna harm the performances.

I know this is a really general question, but any one used this library for production env? and noticed performance issues?

2

There are 2 best solutions below

2
On

Apart from running it multiple times to check for JVM optimization (I don't know if given the fact that Predicate can be a functional interface, the JVM could not use the new bytecode keyword invokedynamic introduced in Java 7), I think you error rely just after the start:

start = System.nanoTime();
filtered.clear();
CollectionUtils.select(ints, predicate,filtered);
end = System.nanoTime();
System.out.println(filtered + " (" + (end - start) + ")");

I don't think you should evaluate the time filtered.clear() does it work if you want to check differences between CollectionUtils and plain old foreach.

0
On

Well, you are basically comparing method invocation overhead with inline code with the latter being obviously faster.

As long as you do not do something that really challenges your cpu, I would be very surprised if this would be the cause of performance problems in your application.