0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

処理をどこまで分けるのが正解か

Last updated at Posted at 2019-10-24

##比較しました

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はsysoutです、別で用意してあります.

処理速度検証.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

##理由
処理をどこで分割するとかでなく
Integer呼んでるから1が最低速
中間操作でandThen使ってるから2が3より遅い

4と5はメソッドかFunctionかの差ですけど

メソッド.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

ということで大差ないけどメソッドのが少しはやいかも…?

    以下をすると遅くなりますね
  • FunctionをandThenでつなげまくる
  • メソッドを細かく分けまくって呼びまくる
  • streamの中間操作が増える
  • CollectersやStringなどを呼ぶ

メソッドとFunctionでたいして速度変わらないなら、Functionはstreamの中間操作のために生まれたのか...?

0
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?