You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Stephen Colebourne <sc...@joda.org> on 2003/01/20 02:37:03 UTC

Re: [collections] performance enhancement for CollectionUtils#countMatches

Your proposed change to countMatches makes sense and I have applied it.

Your proposed addition of exists is more troublesome. I agree that it will
halve the average time to retrieve the result. But it does widen the public
API. I think overall it probably is justified.

Would you like to create a patch for the exists and a suitable test case?

Stephen


From: "Peter Koßek" <Bl...@web.de>
1. I recommend to change the code of the new method
CollectionUtils#countMatches. It unnecessarily creates an ArrayList and adds
elements from the input collection to it.

    public static int countMatches(Collection inputCollection, Predicate
predicate) {
        int count = 0;
        if (inputCollection != null && predicate != null) {
            for (Iterator iter = inputCollection.iterator();
iter.hasNext();) {
                Object item = iter.next();
                if (predicate.evaluate(item)) {
                    count++;
                }
            }
        }
        return count;
    }

2. Furthermore, I suggest to introduce a new method CollectionUtils#exists,
which tells the caller whether at least one element of a collection fulfills
a given predicate. I suggest this because it would cost twice the time to
compute #exists with the help of #countMatches.

  /** Answers true if a predicate is true for at least one element of a
collection
    * @return whether at least one element of the collection matches the
    * predicate or false if none could be found
    */
    public static boolean exists(Collection collection, Predicate predicate)
{
        if (collection != null && predicate != null) {
            for (Iterator iter = collection.iterator(); iter.hasNext();) {
                Object item = iter.next();
                if (predicate.evaluate(item)) {
                    return true;
                }
            }
        }
        return false;
    }

To illustrate this, I have programmed a small benchmark test which compares
4 different kinds of #exists
- using #select over the momentary implementation of #countMatches
- using the recommended implementation of #countMatches
- using the suggested implementation of #exists
- using a traditional solution without the flexibility of predicates

The test output is shown at the bottom of this mail.

Greetings
Peter Koßek

----------------------

Tests with different #exist methods to iterate through a class collection
using predicate #isInterface
The test will be tried out 10000 times, the size of the collection is 1000.
The test machine is a Pentium like processor with 800 MHz on W2K with JDK
1.3.1_06.
To execute aClass.isInterface() 10000000 times, this processor needs 1572
milliseconds.

A - The element fulfilling the predicate is at the beginning of the
collection.
A-1 Testing the existence of a predicate using 'CollectionUtils.select()'.
    size 1000 difference 2974
A-2 Testing the existence of a predicate using
'ExperimentalCollectionUtils.countHits()'.
    size 1000 difference 2824
A-3 Testing the existence of a predicate using
'ExperimentalCollectionUtils.exists()'.
    size 1000 difference 10
A-4 Testing the existence of a predicate in the traditional way.
    size 1000 difference 10

B - The element fulfilling the predicate is before the middle of the
collection.
B-1 Testing the existence of a predicate using 'CollectionUtils.select()'.
    size 1000 difference 2954
B-2 Testing the existence of a predicate using
'ExperimentalCollectionUtils.countHits()'.
    size 1000 difference 2814
B-3 Testing the existence of a predicate using
'ExperimentalCollectionUtils.exists()'.
    size 1000 difference 1382
B-4 Testing the existence of a predicate in the traditional way.
    size 1000 difference 1321

C - The element fulfilling the predicate is at the end of the collection.
C-1 Testing the existence of a predicate using 'CollectionUtils.select()'.
    size 1000 difference 2984
C-2 Testing the existence of a predicate using
'ExperimentalCollectionUtils.countHits()'.
    size 1000 difference 2754
C-3 Testing the existence of a predicate using
'ExperimentalCollectionUtils.exists()'.
    size 1000 difference 2764
C-4 Testing the existence of a predicate in the traditional way.
    size 1000 difference 2643
____________________________________________________________________________
__
Werden Sie kreativ! Bei WEB.DE FreeMail hei?t es jetzt nicht nur schreiben,
sondern auch gestalten. http://freemail.web.de/features/?mc=021142


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>