Java中变量参数方法的性能 [英] Performance of variable argument methods in Java

查看:104
本文介绍了Java中变量参数方法的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Java中具有可变数量参数的方法的性能的问题。

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 static final boolean isIn(int i, int v1, int v2) {
    return (v1 == i) || (v2 == i);
}

public static final boolean isIn(int i, int... values) {
    for (int v : values) {
        if (i == v) {
            return true;
        }
    }
    return false;
}

现在我遇到的第一种方法的版本出现了主要问题到20,30或甚至50个参数。现在那只会伤到眼睛。好的,这是遗留代码,我想用唯一的一个变量参数方法替换它。

Now the main problem comes when I have versions of the first method that go up to 20, 30 or even 50 parameters. Now that just hurts the eyes. Ok, this is legacy code and I'd like to replace all of it with the only one variable arguments method.

知道对性能的影响是什么?编译器是否有机会对第二种方法进行一些优化,以便它或多或少类似于第一种形式?

Any idea what the impact on performance would be? Any chance the compiler does some optimization for the second method so that it resembles the first form more or less?

编辑:好吧,也许我不够清楚。我有50个参数的方法没有性能问题。彼得·劳雷说,这只是可读性。
如果我切换到具有可变数量参数的新方法,我想知道性能问题。

换句话说:什么是最好的方法要做到这一点如果你关心性能?有50个参数的方法或只有一个带变量参数的方法?

Ok, maybe I was not clear enough. I don't have performance problems with the methods with 50 arguments. It's just about readability as Peter Lawrey said. I was wondering about performance problems if I switch to the new method with variable number of arguments.
In other words: what would be the best way to do it if you care about performance? Methods with 50 arguments or the only one method with variable arguments?

推荐答案

我有同样的问题,转向实验。 / p>

I had the same question, and turned to experimentation.

public class ArgTest {

  int summation(int a, int b, int c, int d, int e, int f) {
    return a + b + c + d + e + f;
  }

  int summationVArgs(int... args) {
    int sum = 0;
    for (int arg : args) {
      sum += arg;
    }
    return sum;
  }

  final static public int META_ITERATIONS = 200;
  final static public int ITERATIONS = 1000000;

  static public void main(String[] args) {
    final ArgTest at = new ArgTest();
    for (int loop = 0; loop < META_ITERATIONS; loop++) {
      int sum = 0;
      final long fixedStart = System.currentTimeMillis();
      for (int i = 0; i < ITERATIONS; i++) {
        sum += at.summation(2312, 45569, -9816, 19122, 4991, 901776);
      }
      final long fixedEnd = System.currentTimeMillis();
      final long vargStart = fixedEnd;
      for (int i = 0; i < ITERATIONS; i++) {
        sum += at.summationVArgs(2312, 45569, -9816, 19122, 4991, 901776);
      }
      final long vargEnd = System.currentTimeMillis();
      System.out.printf("%03d:%d Fixed-Args: %d ms\n", loop+1, ITERATIONS, fixedEnd - fixedStart);
      System.out.printf("%03d:%d Vargs-Args: %d ms\n", loop+1, ITERATIONS, vargEnd - vargStart);
    }
    System.exit(0);
  }

}

如果您运行此代码现代JVM(这里是1.8.0_20),您将看到可变数量的参数会导致性能开销,也可能导致内存消耗。

If you run this code on a modern JVM (here 1.8.0_20), you will see that the variable number of arguments cause overhead in performance and possible in memory consumption as well.

我只会发布前25次运行:

I'll only post the first 25 runs:

001:1000000 Fixed-Args: 16 ms
001:1000000 Vargs-Args: 45 ms
002:1000000 Fixed-Args: 13 ms
002:1000000 Vargs-Args: 32 ms
003:1000000 Fixed-Args: 0 ms
003:1000000 Vargs-Args: 27 ms
004:1000000 Fixed-Args: 0 ms
004:1000000 Vargs-Args: 22 ms
005:1000000 Fixed-Args: 0 ms
005:1000000 Vargs-Args: 38 ms
006:1000000 Fixed-Args: 0 ms
006:1000000 Vargs-Args: 11 ms
007:1000000 Fixed-Args: 0 ms
007:1000000 Vargs-Args: 17 ms
008:1000000 Fixed-Args: 0 ms
008:1000000 Vargs-Args: 40 ms
009:1000000 Fixed-Args: 0 ms
009:1000000 Vargs-Args: 89 ms
010:1000000 Fixed-Args: 0 ms
010:1000000 Vargs-Args: 21 ms
011:1000000 Fixed-Args: 0 ms
011:1000000 Vargs-Args: 16 ms
012:1000000 Fixed-Args: 0 ms
012:1000000 Vargs-Args: 26 ms
013:1000000 Fixed-Args: 0 ms
013:1000000 Vargs-Args: 7 ms
014:1000000 Fixed-Args: 0 ms
014:1000000 Vargs-Args: 7 ms
015:1000000 Fixed-Args: 0 ms
015:1000000 Vargs-Args: 6 ms
016:1000000 Fixed-Args: 0 ms
016:1000000 Vargs-Args: 141 ms
017:1000000 Fixed-Args: 0 ms
017:1000000 Vargs-Args: 139 ms
018:1000000 Fixed-Args: 0 ms
018:1000000 Vargs-Args: 106 ms
019:1000000 Fixed-Args: 0 ms
019:1000000 Vargs-Args: 70 ms
020:1000000 Fixed-Args: 0 ms
020:1000000 Vargs-Args: 6 ms
021:1000000 Fixed-Args: 0 ms
021:1000000 Vargs-Args: 5 ms
022:1000000 Fixed-Args: 0 ms
022:1000000 Vargs-Args: 6 ms
023:1000000 Fixed-Args: 0 ms
023:1000000 Vargs-Args: 12 ms
024:1000000 Fixed-Args: 0 ms
024:1000000 Vargs-Args: 37 ms
025:1000000 Fixed-Args: 0 ms
025:1000000 Vargs-Args: 12 ms
...

Even在最好的时候,Vargs-Args从未降到0毫秒。

Even at the best of times, the Vargs-Args never dropped to 0ms.

这篇关于Java中变量参数方法的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆