Ответ 1
AFAIK каждый класс знает все классы, которые он расширяет, и интерфейсы, которые он реализует. Они могут храниться в хэш-наборе, предоставляющем время поиска O (1).
Когда код часто принимает одну и ту же ветвь, стоимость может быть почти устранена, так как ЦП может выполнить код в ветке до того, как он определит, нужно ли это сделать, чтобы ветка делала стоимость почти без изменений.
Поскольку микро-бенчмарк был выполнен 4 года назад, я ожидаю, что последние процессоры и JVM будут намного быстрее.
public static void main(String... args) {
Object[] doubles = new Object[100000];
Arrays.fill(doubles, 0.0);
doubles[100] = null;
doubles[1000] = null;
for (int i = 0; i < 6; i++) {
testSameClass(doubles);
testSuperClass(doubles);
testInterface(doubles);
}
}
private static int testSameClass(Object[] doubles) {
long start = System.nanoTime();
int count = 0;
for (Object d : doubles) {
if (d instanceof Double)
count++;
}
long time = System.nanoTime() - start;
System.out.printf("instanceof Double took an average of %.1f ns%n", 1.0 * time / doubles.length);
return count;
}
private static int testSuperClass(Object[] doubles) {
long start = System.nanoTime();
int count = 0;
for (Object d : doubles) {
if (d instanceof Number)
count++;
}
long time = System.nanoTime() - start;
System.out.printf("instanceof Number took an average of %.1f ns%n", 1.0 * time / doubles.length);
return count;
}
private static int testInterface(Object[] doubles) {
long start = System.nanoTime();
int count = 0;
for (Object d : doubles) {
if (d instanceof Serializable)
count++;
}
long time = System.nanoTime() - start;
System.out.printf("instanceof Serializable took an average of %.1f ns%n", 1.0 * time / doubles.length);
return count;
}
окончательно печатает
instanceof Double took an average of 1.3 ns
instanceof Number took an average of 1.3 ns
instanceof Serializable took an average of 1.3 ns
если я изменил "парные" с помощью
for(int i=0;i<doubles.length;i+=2)
doubles[i] = "";
Я получаю
instanceof Double took an average of 1.3 ns
instanceof Number took an average of 1.6 ns
instanceof Serializable took an average of 2.2 ns
Примечание. Если я изменяю
if (d instanceof Double)
to
if (d != null && d.getClass() == Double.class)
производительность была одинаковой.