История изменений
Исправление DiKeert, (текущая версия) :
Вы еще функции каждый раз заново генерируете. Если сделать compose один раз, все еще быстрее получается.
public class AppTest {
@Test
public void testCompose() throws Exception {
long start = System.currentTimeMillis();
for(Integer i = 0; i < 10000; i++){
simple(i);
}
System.out.format("Simple Time: %s \n", System.currentTimeMillis() - start);
start = System.currentTimeMillis();
Function f = hof();
for(Integer i = 0; i < 10000; i++) {
f.apply(i);
}
System.out.format("HOF Time: %s \n", System.currentTimeMillis() - start);
start = System.currentTimeMillis();
IntUnaryOperator o = oHof();
for(int i = 0; i < 10000; i++) {
o.applyAsInt(i);
}
System.out.format("O HOF Time: %s \n", System.currentTimeMillis() - start);
}
private Integer simple(Integer i) {
return i + 1 + 1 + 1;
}
private Function hof() {
return ((Function<Integer, Integer>)(i) -> i + 1)
.compose((Function<Integer, Integer>) (i) -> i + 1)
.compose((Function<Integer, Integer>)(i) -> i + 1);
}
private IntUnaryOperator oHof() {
return ((IntUnaryOperator) i -> i + 1)
.compose(i -> i + 1)
.compose(i -> i + 1);
}
}
Simple Time: 7
HOF Time: 95
O HOF Time: 24
Исправление DiKeert, :
Вы еще функции каждый раз заново генерируете. Если сделать compose один раз, все еще быстрее получается.
public class AppTest {
@Test
public void testCompose() throws Exception {
long start = System.currentTimeMillis();
for(Integer i = 0; i < 10000; i++){
simple(i);
}
System.out.format("Simple Time: %s \n", System.currentTimeMillis() - start);
start = System.currentTimeMillis();
Function f = hof();
for(Integer i = 0; i < 10000; i++) {
f.apply(i);
}
System.out.format("HOF Time: %s \n", System.currentTimeMillis() - start);
start = System.currentTimeMillis();
IntUnaryOperator o = oHof();
for(int i = 0; i < 10000; i++) {
o.applyAsInt(i);
}
System.out.format("O HOF Time: %s \n", System.currentTimeMillis() - start);
}
private Integer simple(Integer i) {
return i + 1 + 1 + 1;
}
private Function hof() {
return ((Function<Integer, Integer>)(i) -> i + 1)
.compose((Function<Integer, Integer>) (i) -> i + 1)
.compose((Function<Integer, Integer>)(i) -> i + 1);
}
private IntUnaryOperator oHof() {
return ((IntUnaryOperator) i -> i + 1)
.compose(i -> i + 1)
.compose(i -> i + 1);
}
}
Simple Time: 7
HOF Time: 95
O HOF Time: 24
Исходная версия DiKeert, :
Вы еще функции каждый раз заново генерируете. Если сделать compose один раз, все еще быстрее получается.
public class AppTest {
@Test
public void testCompose() throws Exception {
long start = System.currentTimeMillis();
for(Integer i = 0; i < 10000; i++){
simple(i);
}
System.out.format("Simple Time: %s \n", System.currentTimeMillis() - start);
start = System.currentTimeMillis();
Function f = hof();
for(Integer i = 0; i < 10000; i++) {
f.apply(i);
}
System.out.format("HOF Time: %s \n", System.currentTimeMillis() - start);
start = System.currentTimeMillis();
IntUnaryOperator o = oHof();
for(int i = 0; i < 10000; i++) {
o.applyAsInt(i);
}
System.out.format("O HOF Time: %s \n", System.currentTimeMillis() - start);
}
private Integer simple(Integer i) {
return i + 1 + 1 + 1;
}
private Function hof() {
return ((Function<Integer, Integer>)(i) -> i + 1)
.compose((Function<Integer, Integer>) (i) -> i + 1)
.compose((Function<Integer, Integer>)(i) -> i + 1);
}
private IntUnaryOperator oHof() {
return ((IntUnaryOperator) i -> i + 1)
.compose(i -> i + 1)
.compose(i -> i + 1);
}
}
Simple Time: 7 HOF Time: 95 O HOF Time: 24