0

I have a method like this

public void doThis(){
   Predicate<String> p = (arg) -> true;
   doThat(p);
}

Will this have any performance impact as compared to doing it without lambda ? Is it possible to store this lambda expression as a instance variable to make it better ?

user2599052
  • 1,056
  • 3
  • 12
  • 27
  • I can't answer the first part (other than the usual: worry about performance when you have a performance problem to worry about), but re the latter, of course you can: `private Predicate p = (arg) -> true;`. – T.J. Crowder Feb 25 '19 at 09:17
  • Doing what without lambda? This method does effectively nothing. – Henry Feb 25 '19 at 09:17
  • The method `doThis` does not actually do anything except declaring a Predicate that remains unused. Did you want to return it ? – Arnaud Denoyelle Feb 25 '19 at 09:17
  • @Henry added more code – user2599052 Feb 25 '19 at 09:18
  • @T.J.Crowder will it make any difference (good/bad)if I refactor it as a instance variable. – user2599052 Feb 25 '19 at 09:20
  • 2
    It depends on how often you would call that method (and hence create a new predicate). Making it a static class member (no need to access instance scope) is probably faster, but also it probably does not really matter. – tobias_k Feb 25 '19 at 09:22
  • You could also make it a private static Predicate or even better a complete new private static method and use it in dothis(). – Spyros K Feb 25 '19 at 09:28
  • the only way to verify performance assumptions is to measure it ;) Everything else is as reliable as reading tea leaves .. – kleopatra Dec 15 '22 at 16:37

1 Answers1

0

you can see performance:

public static void main(String[] args) {
        System.out.println("Predicate using Lambda");
        System.out.println(System.currentTimeMillis());
        doThis();
        System.out.println(System.currentTimeMillis());

        System.out.println("Predicate using Without Lambda");
        System.out.println(System.currentTimeMillis());
        doThisWithoutLambda();
        System.out.println(System.currentTimeMillis());

        System.out.println("Predicate using Lambda 100000");
        System.out.println(System.currentTimeMillis());
        for (int i = 0; i < 100000; i++) {
            doThis();
        }
        System.out.println(System.currentTimeMillis());

        System.out.println("Predicate using Without Lambda 100000");
        System.out.println(System.currentTimeMillis());
        for (int i = 0; i < 100000; i++) {
            doThisWithoutLambda();
        }
        System.out.println(System.currentTimeMillis());
    }

    public static void doThis() {
        Predicate<String> p = (arg) -> true;

    }

    public static void doThisWithoutLambda() {
        Predicate<String> p = new Predicate<String>() {
            @Override
            public boolean evaluate(String object) {
                return true;
            }
        };

    }

Output :

Predicate using Lambda
1551087600419
1551087600491
Predicate using Without Lambda
1551087600491
1551087600492
Predicate using Lambda 100000
1551087600492
1551087600496
Predicate using Without Lambda 100000
1551087600496
1551087600500

Java lambdas 20 times slower than anonymous classes

Talenel
  • 422
  • 2
  • 6
  • 25
Akash Shah
  • 596
  • 4
  • 17
  • Are you as confused as I am about the 100000 iterations taking _less_ time than the single evaluation of the method? (Besides that method of measurement not being super-precise, I would not be surprised if the compiler optimized the lambda/inner class away as it is never used at all.) – tobias_k Feb 25 '19 at 09:49
  • Lamdas is faster if you have more iterations and only solwer in a first time initialization. – Akash Shah Feb 25 '19 at 10:09
  • 1
    You can't trust the numbers from that code. Why: https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – T.J. Crowder Feb 25 '19 at 10:41