Groovy: closing much slower than methods?

During a game with different sorting algorithms, I was surprised that closing Groovy performed very poorly. I have not yet found a good answer to this question, so now I'm trying to get lucky;) Why is closing Groovy so much slower than traditional methods?

Here is a simple example showing the difference in performance. It creates two lists with random numbers and sorts them in reverse order, measuring the sorting time. On my machine and for 10k elements, it takes 270 ms using closure and only 50 ms using Comparator implementation.

Temporal values ​​are slightly different from the distribution of random numbers. I also tried Groovy 1.7.4 and 1.8.0, seeing a slightly better performance with the latter. But the overall picture remains the same: closing does not work well.

What can I do to improve closing performance? Also, of course, do not use closure;) Am I missing something or should not use closure in Groovy, if performance indicators?

def numberCount = 10000
def random = new Random()
def unorderedList1 = (1..numberCount).collect{random.nextInt()}
def unorderedList2 = (1..numberCount).collect{random.nextInt()}
def timeit = {String message, Closure cl->
    def startTime = System.currentTimeMillis()
    cl()
    def deltaTime = System.currentTimeMillis() - startTime
    println "$message: \ttime: $deltaTime"
}

timeit("compare using closure") {
    def comparator= [ compare: { a,b -> return b <=> a }] as Comparator
    unorderedList1.sort(comparator)
}

timeit("compare using method") {
    Comparator comparator = new MyComparator()
    unorderedList2.sort(comparator)
}

class MyComparator implements Comparator {
    int compare(a, b) {return b <=> a}
}
+5
source share
2 answers

What can I do to improve closing performance?

Wait. JDK7 , invokeDynamic, , , JVM.

+4

Groovy 2.0.5 OpenJDK 1.6 (O6) JDK 1.7 (J7) Ubuntu.

:

  • , :
  • , @CompileStatic:

    def numberCount = 10000
    def random = new Random()
    def unorderedList1 = (1..numberCount).collect{random.nextInt()}
    def unorderedList2 = (1..numberCount).collect{random.nextInt()}
    def unorderedList3 = (1..numberCount).collect{random.nextInt()}
    def unorderedList4 = (1..numberCount).collect{random.nextInt()}
    def timeit = {String message, Closure cl->
        def startTime = System.currentTimeMillis()
        cl()
        def deltaTime = System.currentTimeMillis() - startTime
        println "$message: \ttime: $deltaTime"
    }
    
    timeit("compare using map of closures") {
        def comparator= [ compare: { a,b -> return b <=> a }] as Comparator
        unorderedList1.sort(comparator)
    }
    
    timeit("compare using one closure") {
        def comparator= { a, b -> return b <=> a } as Comparator
        unorderedList2.sort(comparator)
    }
    
    timeit("compare using method") {
        Comparator comparator = new MyComparator()
        unorderedList3.sort(comparator)
    }
    
    timeit("compare using method with @CompileStatic") {
        Comparator comparator = new MyComparator2()
        unorderedList4.sort(comparator)
    }
    
    class MyComparator implements Comparator {
        int compare(a, b) {return b <=> a}
    }
    
    class MyComparator2 implements Comparator<Integer> {
        @groovy.transform.CompileStatic
        int compare(Integer a, Integer b) {return b <=> a}
    }
    
Groovy 2.0.5                groovy  groovyc 
                            O6      O6  J7
closure map                 258     499 146
one closure                 64      205 48
method                      29      37  32
@CompileStatic method       28      26  22

, Groovy, JDK7 (, , OpenJDK6), .

+4

All Articles