Compared to Sum and Summing Int

I tried experimenting

The comparison target is mainly stream operations.

Below is the addition of the numerical list Whether to use mapToInt (v-> v) .sum () Whether to use collect (Collectors.summingInt (Integer :: intValue)))

in addition,

Whether to divide the processing contents of the intermediate operation and the termination operation into separate functions Do you put them all together in one function?

Are comparing.

ProcessingTest.java


package mk42;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

public class ProcessingTest {
	public static void main(String[] args) {
		long startTime = System.nanoTime();
		run(generate.get());
		long endTime = System.nanoTime();
		Mk42.echo.accept("Processing Time : " + (endTime - startTime) + " ns");
	}
	/**
	 * Processing speed test.
	 *
	 * @param list
	 */
	private static void run(List<String> list) {
		try {
			// fastest
			Mk42.echo.accept(list.stream().filter(Objects::nonNull).map(add3.andThen(convertInt)).mapToInt(v -> v).sum());
			// normal
			Mk42.echo.accept(list.stream().filter(Objects::nonNull).map(add3.andThen(convertInt))
					.collect(Collectors.summingInt(Integer::intValue)));
			// bad
			all.accept(list);
		} catch (NumberFormatException e) {
			Mk42.echo.accept(join.apply(list));
		}
	}
	/** A series of processing */
	static Consumer<List<String>> all = v -> {
		Mk42.echo.accept(
				v.stream().filter(Objects::nonNull).map(x -> x + "3").map(Integer::parseInt).mapToInt(z -> z).sum());
	};
	/** Add 3 */
	static UnaryOperator<String> add3 = v -> v + "3";
	/** Convert to Integer */
	static Function<Object, Integer> convertInt = v -> Integer.parseInt(String.valueOf(v));
	/** Join with comma */
	static Function<List<String>, String> join = v -> v.stream().collect(Collectors.joining(","));
	/** Generate List which can convert to Integer */
	static Supplier<List<String>> generate = () -> Arrays.asList(null, "1", "2", "3", "4", "5");
}

By the way, what is Mk42?

Mk42.java


package mk42;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Answer to the Ultimate Question of Life, the Universe, and Everything.
 */
public class Mk42 {
	/** echo */
	public static Consumer<Object> echo = v -> System.out.println(v);
	/** not null */
	public static Predicate<Object> notNull = v -> Optional.ofNullable(v).isPresent();
	/** sum list */
	public static Function<List<Integer>, Integer> sum = v -> v.stream().filter(notNull).mapToInt(i -> i).sum();
	/** generate String list */
	public static Supplier<List<String>> strList = () -> Arrays.asList(null, "", " ", "a", "0");
	/** generate Integer list */
	public static Supplier<List<Integer>> intList = () -> Arrays.asList(null, 1, 2, 3, 4, 5);
}

result

so?

It seems that it is faster to separate the operation functions.

The main thing this time is the termination operation Calling Collectors in collect is slower than mapToInt It seems natural because you are calling an object

It may be a detailed thing, but if it is similar processing, which one is faster I want to remember

Recommended Posts

Compared to Sum and Summing Int
Sum from Java_1 to 100
Compared processing time to convert Apache Arrow and JSON to Yosegi
Gradle to touch and remember