Performance of variable argument methods in Java

前端 未结 6 1152
小鲜肉
小鲜肉 2020-12-19 20:42

I have this question about the performance of a method in Java with a variable number of parameters.

Say I have the following 2 alternatives:

public          


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-19 21:10

    The compiler does next to no optimisation. The JVM can optimise code but the two methods won't perform anything like each other. If you have lines of code like isIn(i, 1,2,3,4,5,6,7,8,9 /* plus 40 more */) you have more than performance issues to worry about IMHO. I would worry about readability first.

    If you are worried about performance pass the arguments as a int[] which is reused.

    BTW The most efficient way to look up a large set of int values is to use a Set like TIntHashSet

提交回复
热议问题