Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one determine the progress of a Java Comparator?

I'm using a Comparator implementation to sort a large collection of objects. Depending on the type of objects in this collection the sort takes a few milliseconds to half a minute. Is there any way to determine the progress of the Comparator while sorting? I'd like to visualize this for the user.

Collections.sort(sorted, new Comparator<Object[]>() {
    public int compare(Object[] o1, Object[] o2) {
        /* do it... */
        return order;
    }
}

The collection may hold simple short String objects, Date objects or (worst case) CLOB objects which need to fetch data while sorting.

like image 771
Daniel Bleisteiner Avatar asked Nov 29 '12 09:11

Daniel Bleisteiner


1 Answers

You could do that by writing an comparator that counts up an "global" variable.

But to visualize the progress for analytical purpose, you have to copy your list, and sort it twice. The first time you determine the number of comparator calls. The next time you know how far you already are, by comparing the current counter with the value from the first sort.

You would need a second thread to read out the counter while the other thread is sorting.

Another possibility is to estimate the number of comparator calls: on average this could be related to n * ld (n).

Then again count up, and read from another thread. This way you have to sort only once.

like image 135
AlexWien Avatar answered Sep 20 '22 12:09

AlexWien