Coding i came around to check for the vararg performance of Java.
I write following test code:
public class T {
public static void main(String[
Using both the latest JRE6 and JRE7 I get different results than yours and they indicate that varargs are 5 times faster:
69
69
311
However, I would not jump to conclusions because this benchmark has several flaws: the parameters are not used in the function; the function doesn't do anything; the arguments have the same value. JIT can easily optimize this code and inline function calls. I modified your example to address the aforementioned obvious problems and got the following results:
627
7470
7844
The conclusion is: don't hesitate to use varargs. If your function is trivial then its call be inlined by the JIT, and if it's not then the overhead of varargs will likely be negligible.
As said, an array is maintained when using var-args...,
you should also try to see the influence of adding "final" to the parameters of every methods
i personally get an improvement from 2250 -> 2234 ms for the array.
Interesting problem !
This is just a guess : behind the scenes, Var-args are just arrays. Creating this implicit array and populating it with the var-args parameters may take some time; hence the performance hit. Well, I guess.
Static list of arguments is quite different from an array. When you pass them that way, compiler reserves space for the references and populates them when the method is called.
Varargs is an equivalent of array. To call such a method, it's necessary to create and populate array at run time. That's why you observe the difference.
String[]
and String...
are synonyms. If you compared them, you should see identical performance.
I refactored some of the code. I use int
now instead of String
cause strings end in a pool and are immutable, and I don't know how that influences the compiler optimizations.
Also I recreate the values so they won't be constants which might also influence compiler optimizations.
And I do something with the methods, also against compiler optimizations.
public class Test {
static int n = 100_000_000;
static int[] all_string = new int[n*1*5];
static int all_strings_index = 0;
public static void main(String[] args) {
while(true) {
all_strings_index = 0;
int s1 = (int) System.nanoTime();
int s2 = (int) System.nanoTime();
int s3 = (int) System.nanoTime();
int s4 = (int) System.nanoTime();
int s5 = (int) System.nanoTime();
long t = System.currentTimeMillis();
t = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
var(s1, s2, s3, s4, s5);
}
System.err.println("varargs "+(System.currentTimeMillis() - t));
all_strings_index = 0;
t = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
par(s1, s2, s3, s4, s5);
}
System.err.println("parameters "+(System.currentTimeMillis() - t));
all_strings_index = 0;
int[] arr = new int[] {s1, s2, s3, s4, s5};
t = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
var2(arr);
}
System.err.println("array "+(System.currentTimeMillis() - t));
System.err.println();
}
}
static void par(int a1, int a2, int a3, int a4, int a5) {
all_string[all_strings_index++] = a1;
all_string[all_strings_index++] = a2;
all_string[all_strings_index++] = a3;
all_string[all_strings_index++] = a4;
all_string[all_strings_index++] = a5;
}
static void var(int... a) {
for (int s : a) {
all_string[all_strings_index++] = s;
}
}
static void var2(int[] a) {
for (int s : a) {
all_string[all_strings_index++] = s;
}
}
}
varargs 981
parameters 415
array 687
varargs 962
parameters 434
array 411
varargs 975
parameters 469
array 439
varargs 983
parameters 462
array 447
varargs 999
parameters 470
array 439
varargs 1018
parameters 475
array 455
varargs 1014
parameters 467
array 440
varargs are a lot slower.