MethodHandle performance

前端 未结 3 2101
攒了一身酷
攒了一身酷 2021-02-07 02:30

I wrote a little benchmark that tests performance of java.lang.invoke.MethodHandle, java.lang.reflect.Method and direct calls of methods.

I re

3条回答
  •  滥情空心
    2021-02-07 03:07

    It appears others have seen similar results: http://vanillajava.blogspot.com/2011/08/methodhandle-performance-in-java-7.html

    Here's someone else's: http://andrewtill.blogspot.com/2011/08/using-method-handles.html

    I ran that second one and saw they were about the same speed even fixing that test to have a warmup. However, I fixed it so it wasn't creating an args array every time. At the default count, it resulted in the same result: methodhandles were a little faster. But I did a count of 10000000 (default*10) and reflection went much faster.

    So, I would recommend testing with parameters. I wonder if MethodHandles more efficiently deal with parameters? Also, check changing the count--how many iterations.

    @meriton's comment on the question links to his work and looks very helpful: Calling a getter in Java though reflection: What's the fastest way to repeatedly call it (performance and scalability wise)?

提交回复
热议问题