How far is the correct answer to divide the process?

Compared

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
	 */
}

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

Reason

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

How far is the correct answer to divide the process?
Customize how to divide the contents of Recyclerview
How is the next value of the Time object correct?
How to perform a specific process when the back button is pressed in Android Fragment
How to use the link_to method
How to use the include? method
How to find the average angle
How to use the wrapper class
How to output the value when there is an array in the array
I tried to understand how the rails method "redirect_to" is defined
I tried to understand how the rails method "link_to" is defined
I examined the concept of the process to understand how Docker works
How to identify the path that is easy to make a mistake
How to add the delete function
How to write the view when Vue is introduced in Rails?
[Docker] How to build when the source code is bind-mounted on the container
[Ruby] How to prevent errors when nil is included in the operation
[Java] How to use the File class
How to delete the wrong migration file
[Java] How to use the hasNext function
How to put out the error bundling
[Java] How to use the HashMap class
How to delete the migration file NO FILE
[Rails] How to use the map method
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to determine the number of parallels
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Swift] How to implement the countdown function
How to change the timezone on Ubuntu
How the website is displayed on the screen
Ransack sort_link How to change the color!
[Processing × Java] How to use the class
How to sort the List of SelectItem
How to install the legacy version [Java]
How to find the tens and ones
How to pass the value to another screen
How to get the date in java
[Java 7] Divide the Java list and execute the process
[Processing × Java] How to use the function
The process of introducing Vuetify to Rails
[Java] How to use the Calendar class
Summarized how to climb the programming stairs
[swift] How to control the behavior when the back button of NavigationBar is pressed
If the parameter is an array, how to include it in Stopara's params.permit
How to resolve the "OpenSSH keys only supported if ED25519 is available" error
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
How to set when "The constructor Empty () is not visible" occurs in junit
How to find the cause of the Ruby error
How to use the camera module OV7725 (ESP32-WROVER-B)
How to write offline 15th reference question answer
How to check the logs in the Docker container
[Java] How to use Thread.sleep to pause the program
[Swift] How to implement the LINE login function
[swift5] How to implement the Twitter share function
How to add sound in the app (swift)
How to implement the breadcrumb function using gretel
[Swift] How to link the app with Firebase
How to get today's day of the week