Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
> java - version
java version "1.7.0_01"
Java(TM) SE Runtime Environment (build 1.7.0_01-b08)
Java HotSpot(TM) 64-Bit Server VM (build 21.1-b02, mixed mode)
> java -Xms1G -Xmx1G FieldArrayTest
Field: allocation = 422, increment = 187
Array: allocation = 562, increment = 265
Field: allocation = 405, increment = 187
Array: allocation = 577, increment = 250
Field: allocation = 421, increment = 203
Array: allocation = 546, increment = 265
Field: allocation = 422, increment = 171
Array: allocation = 561, increment = 265
Field: allocation = 421, increment = 203
Array: allocation = 546, increment = 266
> java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b11)
Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)
> java -Xms1G -Xmx1G -client FieldArrayTest
Field: allocation = 924, increment = 227
Array: allocation = 972, increment = 314
Field: allocation = 924, increment = 227
Array: allocation = 977, increment = 307
Field: allocation = 930, increment = 246
Array: allocation = 1014, increment = 308
Field: allocation = 911, increment = 225
Array: allocation = 968, increment = 310
Field: allocation = 926, increment = 234
Array: allocation = 970, increment = 321
>java -version
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
> java -Xms1G -Xmx1G FieldArrayTest
Field: allocation = 365, increment = 171
Array: allocation = 494, increment = 253
Field: allocation = 364, increment = 161
Array: allocation = 485, increment = 225
Field: allocation = 363, increment = 168
Array: allocation = 494, increment = 221
Field: allocation = 385, increment = 160
Array: allocation = 489, increment = 222
Field: allocation = 358, increment = 169
Array: allocation = 490, increment = 225
final int count = 100;
IntNumField[] fieldsArray = new IntNumField[count];
int[][] arraysArray = new int[count][];
for (int c = 0; c < count; c++) {
fieldsArray[c] = new IntNumField();
arraysArray[c] = IntNumArray2.create();
}
System.gc();
Thread.sleep(1000);
for (int c = 0; c < count; c++) {
long time1 = System.nanoTime();
test(fieldsArray[c]);
time1 = System.nanoTime() - time1;
long time2 = System.nanoTime();
test(arraysArray[c]);
time2 = System.nanoTime() - time2;
System.out.printf("A/F: %+d %%%n", (time2 - time1) * 100L / time1);
}
A/F: +0 %
A/F: -3 %
A/F: +5 %
A/F: -11 %
...
A/F: +0 %
A/F: -3 %
A/F: +0 %
A/F: +1 %
private volatile int[] array = new int[10000];
...
public void test(){
int s = 0;
for(int i=0; i<array.length; i++) {
s+=array[i];
}
sum = s;
}
Throughput = 62 ops/msec.
void method(String... args) {
// do something
}
void user() {
for(int count=0; counter<1000; counter++) method(); // this is slow
for(int count=1000; counter-->0;) method((String[])null)// this is fast
}
THIS IS SLOW
for(int count=0; counter<1000; counter++) method();
тут используется постинкремент и неявное создание пустого массиваTHIS IS FAST
for(int count=1000; counter-->0;) method((String[])null)// this is fast
тут используется еще одна «оптимизация», сравнение с нулем быстрее чем с 1000. И не создается лишний мусор.{
int i=10;
}
{
int x;
println(x);
}
В ответ на вопрос почему доступ к элементу массива быстрее чем доступ к полям объекта