Git Product home page Git Product logo

java-method-invoke-benchmark-test's Introduction

java-method-invoke-benchmark-test

A simple benchmark of java method invocation in various ways.

Method invocation ways

We have 4 ways to invoke a method in java:

  1. Directly invoke the method.
  2. Use reflection to invoke the method.
  3. Use MethodHandle to invoke the method.
  4. Use bound MethodHandle to invoke the method.(By MethodHandle::bindTo)

Environment

  • OS: Windows 11
  • CPU: 13th Gen Intel(R) Core(TM) i9-13900H 2.60 GHz
  • RAM: 32GB
  • JDK: java -version
    java version "1.8.0_391"
    Java(TM) SE Runtime Environment (build 1.8.0_391-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 25.391-b13, mixed mode)
    

Benchmark result

Warming up...
==================================================
Test results of 100*100=10000 rounds:
Direct:                                 453100ns
Bound MethodHandle:                     1253400ns
MethodHandle:                           1089900ns
Reflect:                                4138200ns

Bound/Handle/Reflect - Direct: 
Bound - Direct:                         800300
Handle - Direct:                        636800
Reflect - Direct:                       3685100

Handle/Reflect VS Bound MethodHandle: 
Handle - Bound / (Bound - Direct):      -0.20429838810446083
Reflect - Bound / (Bound - Direct):     3.604648256903661

Reflect VS MethodHandle: 
Reflect - Handle / (Handle - Direct):   4.786903266331659
==================================================
Warm up finished.

==================================================
Test results of 10*10000=100000 rounds:
Direct:                                 2866500ns
Bound MethodHandle:                     8271100ns
MethodHandle:                           7342400ns
Reflect:                                7124000ns

Bound/Handle/Reflect - Direct: 
Bound - Direct:                         5404600
Handle - Direct:                        4475900
Reflect - Direct:                       4257500

Handle/Reflect VS Bound MethodHandle: 
Handle - Bound / (Bound - Direct):      -0.17183510343041114
Reflect - Bound / (Bound - Direct):     -0.212245124523554

Reflect VS MethodHandle: 
Reflect - Handle / (Handle - Direct):   -0.04879465582340982
==================================================

==================================================
Test results of 10*100000=1000000 rounds:
Direct:                                 23403300ns
Bound MethodHandle:                     28199700ns
MethodHandle:                           27949000ns
Reflect:                                33373200ns

Bound/Handle/Reflect - Direct: 
Bound - Direct:                         4796400
Handle - Direct:                        4545700
Reflect - Direct:                       9969900

Handle/Reflect VS Bound MethodHandle: 
Handle - Bound / (Bound - Direct):      -0.052268367942623635
Reflect - Bound / (Bound - Direct):     1.0786214660995748

Reflect VS MethodHandle: 
Reflect - Handle / (Handle - Direct):   1.1932595639835448
==================================================

==================================================
Test results of 10*1000000=10000000 rounds:
Direct:                                 218572400ns
Bound MethodHandle:                     231361400ns
MethodHandle:                           226761800ns
Reflect:                                245221100ns

Bound/Handle/Reflect - Direct: 
Bound - Direct:                         12789000
Handle - Direct:                        8189400
Reflect - Direct:                       26648700

Handle/Reflect VS Bound MethodHandle: 
Handle - Bound / (Bound - Direct):      -0.35965282664790055
Reflect - Bound / (Bound - Direct):     1.0837203847056063

Reflect VS MethodHandle: 
Reflect - Handle / (Handle - Direct):   2.2540479155982123
==================================================

==================================================
Test results of 10*10000000=100000000 rounds:
Direct:                                 2025875200ns
Bound MethodHandle:                     2114698200ns
MethodHandle:                           2111815500ns
Reflect:                                2201683500ns

Bound/Handle/Reflect - Direct: 
Bound - Direct:                         88823000
Handle - Direct:                        85940300
Reflect - Direct:                       175808300

Handle/Reflect VS Bound MethodHandle: 
Handle - Bound / (Bound - Direct):      -0.03245443184760704
Reflect - Bound / (Bound - Direct):     0.979310538936987

Reflect VS MethodHandle: 
Reflect - Handle / (Handle - Direct):   1.045702656378905
==================================================

==================================================
Test results of 10*100000000=1000000000 rounds:
Direct:                                 19686971700ns
Bound MethodHandle:                     20425297900ns
MethodHandle:                           20391888000ns
Reflect:                                21605664900ns

Bound/Handle/Reflect - Direct: 
Bound - Direct:                         738326200
Handle - Direct:                        704916300
Reflect - Direct:                       1918693200

Handle/Reflect VS Bound MethodHandle: 
Handle - Bound / (Bound - Direct):      -0.04525086608060232
Reflect - Bound / (Bound - Direct):     1.5987066421318923

Reflect VS MethodHandle: 
Reflect - Handle / (Handle - Direct):   1.7218737884199868
==================================================

java-method-invoke-benchmark-test's People

Watchers

Yunie avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.