FUnction.java
package mk42;
import java.util.List;
import java.util.function.Function;
import java.util.function.UnaryOperator;
/**
* All method has the same process.
*
* @author
*
*/
public class NotStaticProcessingTest {
/**
* This method contains all process to need.
* @param list
*/
public void streamTest1(List<String> list) {
list.stream().map(v -> v + "3").mapToInt(Integer::parseInt).sum();
}
/**
* This method has 2 functions.
* @param list
*/
public void streamTest2(List<String> list) {
list.stream().map(add3.andThen(convertInt)).mapToInt(v -> v).sum();
}
/**
* This method has 1 functions.
* @param list
*/
public void streamTest3(List<String> list) {
list.stream().map(all).mapToInt(v -> v).sum();
}
/**
* This method has 1 function.
* The function has all process.
* @param list
*/
public void streamTest4(List<String> list) {
this.sum.apply(list);
}
/**
* This method has methos instead of function.
* @param list
*/
public void streamTest5(List<String> list) {
this.sum(list);
}
/** Add 3 */
private UnaryOperator<String> add3 = v -> v + "3";
/** Convert to Integer */
private Function<Object, Integer> convertInt = v -> Integer.parseInt(String.valueOf(v));
/** all */
private Function<String, Integer> all = v -> Integer.parseInt(v + "3");
/** all of them function */
private Function<List<String>, Integer> sum = v -> v.stream().mapToInt(x -> Integer.parseInt(x + "3")).sum();
/** all of them method */
private Integer sum(List<String> list) {
return list.stream().mapToInt(v -> Integer.parseInt(v + "3")).sum();
}
}
Mk42.echo is sysout, it is prepared separately.
Processing speed verification.java
package mk42;
import java.util.Arrays;
import java.util.List;
public class StreamProcessingTest {
/** constant list */
private static final List<String> list = Arrays.asList("1", "2", "3");
/**
* main
* @param args
*/
public static void main(String[] args) {
// instance
NotStaticProcessingTest no = new NotStaticProcessingTest();
/** ----------test1---------- */
Mk42.echo.accept("stream test 1");
Long start1 = System.nanoTime();
no.streamTest1(list);
Long end1 = System.nanoTime();
Mk42.echo.accept("Processing TIme : " + (end1 - start1) + "ns");
/** ----------test2---------- */
Mk42.echo.accept("stream test 2");
Long start2 = System.nanoTime();
no.streamTest2(list);
Long end2 = System.nanoTime();
Mk42.echo.accept("Processing TIme : " + (end2 - start2) + "ns");
/** ----------test3---------- */
Mk42.echo.accept("stream test 3");
Long start3 = System.nanoTime();
no.streamTest3(list);
Long end3 = System.nanoTime();
Mk42.echo.accept("Processing TIme : " + (end3 - start3) + "ns");
/** ----------test4---------- */
Mk42.echo.accept("stream test 4");
Long start4 = System.nanoTime();
no.streamTest4(list);
Long end4 = System.nanoTime();
Mk42.echo.accept("Processing TIme : " + (end4 - start4) + "ns");
/** ----------test5---------- */
Mk42.echo.accept("stream test 5");
Long start5 = System.nanoTime();
no.streamTest5(list);
Long end5 = System.nanoTime();
Mk42.echo.accept("Processing TIme : " + (end5 - start5) + "ns");
}
/**
* As a result.
*
* stream test 1
* Processing TIme : 3630700ns
*
* stream test 2
* Processing TIme : 632701ns
*
* stream test 3
* Processing TIme : 195400ns
*
* stream test 4
* Processing TIme : 151499ns
*
* stream test 5
* Processing TIme : 143600ns
*/
}
stream test 1
Processing TIme : 3630700ns
stream test 2
Processing TIme : 632701ns
stream test 3
Processing TIme : 195400ns
stream test 4
Processing TIme : 151499ns
stream test 5
Processing TIme : 143600ns
Not where to divide the process 1 is the fastest because I'm calling Integer 2 is slower than 3 because I use andThen in the middle operation
4 and 5 are the difference between method and function
Method.java
public void streamTest4(List<String> list) {
this.sum.apply(list);
}
public void streamTest5(List<String> list) {
this.sum(list);
}
stream test 4
Processing TIme : 151499ns
stream test 5
Processing TIme : 143600ns
So it doesn't make much difference, but the method may be a little faster ...?
If the speed doesn't change much between the method and the Function, was the Function born for an intermediate operation of stream ...?
Recommended Posts