2

I had this piece of code below:

 PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<>(dscsortedMAP.size(), new Comparator<Map.Entry<Integer, Integer>>() {

        @Override
        public int compare(Map.Entry<Integer, Integer> arg0, Map.Entry<Integer, Integer> arg1) {

            return arg1.getValue().compareTo(arg0.getValue());
        }
    });

Than IntelliJ IDEA friendly suggested that I can replace the code above to a lambda expression like below:

    PriorityQueue<Map.Entry<Integer, Integer>> pq = new PriorityQueue<>(dscsortedMAP.size(), (arg0, arg1) -> {

        return arg1.getValue().compareTo(arg0.getValue());
    });

Well guess what happened, in my method the execution time of the 2. code executed 20 times slower?! First version took 7 miliseconds to sort the list, and after lambda expression it was 140 miliseconds?!

Am I missing something here? I didn't test if the execuation time proportionally increases as data grows. maybe thats just the initial one time cpu time?

Spring
  • 11,333
  • 29
  • 116
  • 185
  • Look what you did now: you edited your post, so my answer does not make sense anymore. Do you still get this difference in timing now that the code in both functions is the same? – Mike Nakis Oct 09 '16 at 13:03

1 Answers1

3

The first time a lambda is used, the JVM has to generate the byte code of the class and load it. The first time any lambda is used, most of the lambda code generating library is loaded.

I have found that using a Lambda Comparator can be slower. It is likely that in future version this will not be the case. More importantly, lambda code definitely needs to be loaded and warmed up to be fast.

Your code isn't run long enough to say. I would warm the code up for at least 2 - 10 seconds before taking any measurement.

When you use a Comparator to sort a large list it can be called N log2 N times which is a lot. Any inefficiency with show up as significant.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • thank you. Could you tell what would be better to use to sort a large list instead of comparator? Actually I can create another question for this with more code if you would like to. Because i have a performance issue on sorting here. – Spring Oct 09 '16 at 11:12
  • Lambda byte code is generated at compile time just like any other anonymous class. JVM does not generate any code – Jack Oct 09 '16 at 11:32
  • @Spring I would try both and see. – Peter Lawrey Oct 09 '16 at 11:34
  • @Jack the class you get from lambda.getClass() is generated at runtime and the name of that class depends on the version of the JVM. – Peter Lawrey Oct 09 '16 at 11:37
  • @PeterLawrey thanks. I can't figure out the reason - does anyone know? – Jack Oct 09 '16 at 12:32
  • @Jack They want the ability to change how lambdas are implemented in the future. They have already changed the class name which is used. This wouldn't be possible if you got the `javac` to create the class. – Peter Lawrey Oct 09 '16 at 12:34