Why is Java faster with a function using an external variable containing X rather than using X directly?

Crystark

So i've made the following benchmark to try and understand how Lambas impact performance.

@Fork(1)
@Measurement(iterations = 5)
@Warmup(iterations = 5)
public class LambdaBenchmark {

    @State(Scope.Thread)
    public static class MyInteger {
        public Integer value = 10;
    }

    @Benchmark
    public void TestValueInside(MyInteger integer) {
        Function<Integer, Integer> toTest = i -> i + 10;
        toTest.apply(1);
    }

    @Benchmark
    public void TestValueOutside(MyInteger integer) {
        Function<Integer, Integer> toTest = i -> i + integer.value;
        toTest.apply(1);
    }

    @Benchmark
    public void TestValueOutsideFinal(MyInteger integer) {
        int i2 = 10;
        Function<Integer, Integer> toTest = i -> i + i2;
        toTest.apply(1);
    }

    @Benchmark
    public void TestValueOutsideLocalCopy(MyInteger integer) {
        int i2 = integer.value;
        Function<Integer, Integer> toTest = i -> i + i2;
        toTest.apply(1);
    }
}

I'm kinda puzzled by the results:

Benchmark                                   Mode  Cnt           Score           Error  Units
LambdaBenchmark.TestValueInside            thrpt    5  1494683335,686 ▒ 157769032,327  ops/s
LambdaBenchmark.TestValueOutside           thrpt    5   755197977,631 ▒  39587849,696  ops/s
LambdaBenchmark.TestValueOutsideFinal      thrpt    5  3007751583,191 ▒ 178696557,885  ops/s
LambdaBenchmark.TestValueOutsideLocalCopy  thrpt    5   771307179,267 ▒  13613431,113  ops/s

Why is TestValueOutsideFinal so much faster than TestValueInside ? We're using an external variable that may be considered final but it's still a variable rather than a direct value ? Or is the value 10 constantly being recreated rather than always using the same addressed variable ?

EDIT:

After taking into account what @AlBlue said, it's indeed showing much closer results.

Here are the results once I return each value:

Benchmark                                   Mode  Cnt          Score          Error  Units
LambdaBenchmark.TestValueInside            thrpt    5  309129197,389 ▒ 32089680,994  ops/s
LambdaBenchmark.TestValueOutside           thrpt    5  283435336,319 ▒ 52230809,938  ops/s
LambdaBenchmark.TestValueOutsideFinal      thrpt    5  360590518,854 ▒  3072257,599  ops/s
LambdaBenchmark.TestValueOutsideLocalCopy  thrpt    5  279159794,477 ▒ 12871790,409  ops/s
AlBlue

Your code is falling for the oldest problem in benchmarking: you are ignoring the results of the methods. As a result everything is being thrown away and you're measuring random data.

Always, always, always return the value from the function call, or have it consumed with Blackhole.consume. Otherwise you won't get what you expect you are measuring.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why is 'x' in ('x',) faster than 'x' == 'x'?

From Dev

Why is `-1 * x` faster than `-x` and why?

From Dev

Get scancode rather than keycode on Linux using X11

From Java

Why is list(x for x in a) faster for a=[0] than for a=[]?

From Dev

In OS X, why does using println() cause my program to run faster than without println()

From Dev

Why is the sum of reciprocals using a for-loop ~400x faster than streams?

From Dev

Why does my python script run much faster when using 5 CPUs rather than 12?

From Dev

Using a function in Sass is returning the string containing the name of the function rather than the result

From Dev

Using a function in Sass is returning the string containing the name of the function rather than the result

From Dev

Why do most relational databases write to logs rather than directly to disk using memory mapping?

From Dev

Why do most relational databases write to logs rather than directly to disk using memory mapping?

From Dev

Why is my program faster than the one using a python built in function?

From Dev

Why is my program faster than the one using a python built in function?

From Dev

Why is function composition from left to right 11x to 19x faster than right to left?

From Dev

Polymorphism - Using an object that inherits from an interface rather than the interface directly

From Dev

Advantage of using a SQL function rather than a SELECT

From Dev

why storing data directly using print() method is faster than storing it in a string and then writing to a file?

From Dev

Why is using an AST faster than not using one?

From Dev

why is using .index() faster than using a for loop?

From Dev

Why new Function(code) is faster than directly executing the same code?

From Dev

Using the built-in "time" command in bash rather than the external command

From Java

Why is x**4.0 faster than x**4 in Python 3?

From Dev

Why is x**4.0 faster than x**4 in Python 3?

From Dev

bash script copy output from set -x to file, called from within script rather than using tee

From Dev

Swig wrapping C++ to python is using default Python rather than Anaconda python in Mac OS X

From Dev

How to throw an exception up to the caller, rather than handle it, using RxJava 1.x?

From Dev

Why is it faster to break rather than to raise an exception?

From Dev

Why is X Server a server rather than a library with a set of predefined functions?

From Dev

Is using arithmetic faster than storing a variable?

Related Related

  1. 1

    Why is 'x' in ('x',) faster than 'x' == 'x'?

  2. 2

    Why is `-1 * x` faster than `-x` and why?

  3. 3

    Get scancode rather than keycode on Linux using X11

  4. 4

    Why is list(x for x in a) faster for a=[0] than for a=[]?

  5. 5

    In OS X, why does using println() cause my program to run faster than without println()

  6. 6

    Why is the sum of reciprocals using a for-loop ~400x faster than streams?

  7. 7

    Why does my python script run much faster when using 5 CPUs rather than 12?

  8. 8

    Using a function in Sass is returning the string containing the name of the function rather than the result

  9. 9

    Using a function in Sass is returning the string containing the name of the function rather than the result

  10. 10

    Why do most relational databases write to logs rather than directly to disk using memory mapping?

  11. 11

    Why do most relational databases write to logs rather than directly to disk using memory mapping?

  12. 12

    Why is my program faster than the one using a python built in function?

  13. 13

    Why is my program faster than the one using a python built in function?

  14. 14

    Why is function composition from left to right 11x to 19x faster than right to left?

  15. 15

    Polymorphism - Using an object that inherits from an interface rather than the interface directly

  16. 16

    Advantage of using a SQL function rather than a SELECT

  17. 17

    why storing data directly using print() method is faster than storing it in a string and then writing to a file?

  18. 18

    Why is using an AST faster than not using one?

  19. 19

    why is using .index() faster than using a for loop?

  20. 20

    Why new Function(code) is faster than directly executing the same code?

  21. 21

    Using the built-in "time" command in bash rather than the external command

  22. 22

    Why is x**4.0 faster than x**4 in Python 3?

  23. 23

    Why is x**4.0 faster than x**4 in Python 3?

  24. 24

    bash script copy output from set -x to file, called from within script rather than using tee

  25. 25

    Swig wrapping C++ to python is using default Python rather than Anaconda python in Mac OS X

  26. 26

    How to throw an exception up to the caller, rather than handle it, using RxJava 1.x?

  27. 27

    Why is it faster to break rather than to raise an exception?

  28. 28

    Why is X Server a server rather than a library with a set of predefined functions?

  29. 29

    Is using arithmetic faster than storing a variable?

HotTag

Archive