[JAVA] Old and new speed showdown

Speed showdown

It may not be old or new w Mainly about loop processing, speed confrontation. I played up to 3 times.

size and isEmpty

ListSize.java


package test;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * Test.
 *
 * @author me
 *
 */
public class ListSizeTest {
	/**
	 *measurement.
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("-----size-----");
		Long start1 = System.nanoTime();
		loop1();
		Long end1 = System.nanoTime();
		System.out.println(end1 - start1 + "ns\r\n\r\n-----isEmpty-----");
		Long start2 = System.nanoTime();
		loop2();
		Long end2 = System.nanoTime();
		System.out.println(end2 - start2 + "ns");
	}
	/**
	 * size
	 */
	private static void loop1() {
		List<Integer> list = new LinkedList<Integer>();
		IntStream.range(0, 100000).forEach(i -> list.add(0));
		if (list.size() == 0) {

		}
	}
	/**
	 * isEmpty
	 */
	private static void loop2() {
		List<Integer> list = new LinkedList<Integer>();
		IntStream.range(0, 100000).forEach(i -> list.add(0));
		if (list.isEmpty()) {

		}
	}

	/**
	 * -----size-----
	 * 48341618ns
	 *
	 * -----isEmpty-----
	 * 10812354ns
	 */
}


isEmpty's overwhelming victory ArrayList I don't want to use it as much as possible unless I can't help but want to know the number of elements.

Specified number of loops

for and IntStream.java


package test;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * Test.
 *
 * @author me
 *
 */
public class LoopTest1 {
	/**
	 *measurement.
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("-----for statement-----");
		Long start1 = System.nanoTime();
		loop1();
		Long end1 = System.nanoTime();
		System.out.println(end1 - start1 + "ns\r\n\r\n-----IntStream-----");
		Long start2 = System.nanoTime();
		loop2();
		Long end2 = System.nanoTime();
		System.out.println(end2 - start2 + "ns");
	}
	/**
	 *for statement
	 */
	private static void loop1() {
		List<Integer> list = new LinkedList<Integer>();
		for (int i = 0; i < 10000000; i++) {
			list.add(0);
		}
	}
	/**
	 * IntStream
	 */
	private static void loop2() {
		List<Integer> list = new LinkedList<Integer>();
		IntStream.range(0, 10000000).forEach(i -> list.add(0));
	}
	/**
	 * -----for statement-----
	 * 1563850086ns
	 * 1563641080ns
	 * 1802331635ns
	 *
	 * -----IntStream-----
	 * 1809124813ns
	 * 1726875157ns
	 * 1476842733ns
	 */
}

Hmm ... Well, I haven't verified it exactly, but maybe it's just a for statement. IntStream seems to have high readability Is it rather an intermediate operation of stream?

Extended for statement and stream

This is my favorite

Extended for statement and stream.java


package test;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * Test.
 *
 * @author me
 *
 */
public class LoopTest2 {
	/**
	 *measurement.
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("-----for statement-----");
		Long start1 = System.nanoTime();
		loop1();
		Long end1 = System.nanoTime();
		System.out.println(end1 - start1 + "ns\r\n\r\n-----stream-----");
		Long start2 = System.nanoTime();
		loop2();
		Long end2 = System.nanoTime();
		System.out.println(end2 - start2 + "ns");
	}
	/**
	 *for statement
	 */
	private static void loop1() {
		List<Integer> list = new LinkedList<Integer>();
		IntStream.range(0, 10000000).forEach(i -> list.add(0));
		for(Integer num : list) {
			num += 1;
		}
	}
	/**
	 * stream
	 */
	private static void loop2() {
		List<Integer> list = new LinkedList<Integer>();
		IntStream.range(0, 10000000).forEach(i -> list.add(0));
		list.stream().map(num -> num += 1);
	}
	/**
	 * -----for statement-----
	 * 1746784426ns
	 * 1855868091ns
	 * 1790482647ns
	 *
	 * -----stream-----
	 * 1453116296ns
	 * 1630589874ns
	 * 1487128838ns
	 */
}

In some article, I've seen something like stream, ok for for statement if the number of loops is small. Well, it's fun to use stream

for and extension for

for statement and extended for statement.java


package test;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * Test.
 *
 * @author me
 *
 */
public class LoopTest3 {
	/**
	 *measurement.
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("-----for statement-----");
		Long start1 = System.nanoTime();
		loop1();
		Long end1 = System.nanoTime();
		System.out.println(end1 - start1 + "ns\r\n\r\n-----Extended for statement-----");
		Long start2 = System.nanoTime();
		loop2();
		Long end2 = System.nanoTime();
		System.out.println(end2 - start2 + "ns");
	}
	/**
	 *for statement List#get()
	 */
	private static void loop1() {
		List<Integer> list = new LinkedList<Integer>();
		IntStream.range(0, 100000).forEach(i -> list.add(0));
		for (int i = 0; i < list.size(); i++) {
			int a = list.get(i);
		}
	}
	/**
	 *Extended for statement
	 */
	private static void loop2() {
		List<Integer> list = new LinkedList<Integer>();
		IntStream.range(0, 100000).forEach(i -> list.add(0));
		for (Integer num : list) {
			int a = num;
		}
	}
	/**
	 * -----for statement-----
	 * 5172240485ns
	 *
	 * -----Extended for statement-----
	 * 25571106ns
	 */
}

Yes. There aren't many situations where you can't use the extended for statement. It feels like an extended for statement.

Loop with intermediate operation

Loop with intermediate processing.java


package test;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Test.
 *
 * @author me
 *
 */
public class LoopTest4 {
	/**
	 *measurement.
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("-----for statement-----");
		Long start1 = System.nanoTime();
		loop1();
		Long end1 = System.nanoTime();
		System.out.println(end1 - start1 + "ns\r\n\r\n-----stream-----");
		Long start2 = System.nanoTime();
		loop2();
		Long end2 = System.nanoTime();
		System.out.println(end2 - start2 + "ns");
	}
	/**
	 *for statement
	 */
	private static void loop1() {
		List<String> list = new LinkedList<String>();
		IntStream.range(0, 5000000).forEach(v -> list.add("a"));
		IntStream.range(0, 5000000).forEach(v -> list.add("z"));
		List<String> answer = new LinkedList<String>();
		for (String str : list) {
			if (str.equals("a")) {
				answer.add(str);
			}
		}
	}
	/**
	 * stream
	 */
	private static void loop2() {
		List<String> list = new LinkedList<String>();
		IntStream.range(0, 5000000).forEach(v -> list.add("a"));
		IntStream.range(0, 5000000).forEach(v -> list.add("z"));
		list.stream().filter(v -> v.equals("a")).collect(Collectors.toList());
	}
	/**
	 * -----for statement-----
	 * 2954071288ns
	 * 2845232552ns
	 * 2849512987ns
	 *
	 * -----stream-----
	 * 2036753887ns
	 * 2563286049ns
	 * 2188877495ns
	 */
}

I think it depends on the contents of the intermediate operation, but it seems that it can not be said unconditionally. Well, is it a stream?

Methods and functions

Methods and functions.java


package test;

import java.util.LinkedList;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.IntStream;

/**
 * Test
 * @author me
 *
 */
public class MethodFunctionTest1 {
	/**
	 *measurement
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("-----method-----");
		Long start1 = System.nanoTime();
		method();
		Long end1 = System.nanoTime();
		System.out.println(end1 - start1 + "ns\r\n\r\n-----function-----");
		Long start2 = System.nanoTime();
		function.get();
		Long end2 = System.nanoTime();
		System.out.println(end2 - start2 + "ns");
	}
	/**
	 * method
	 */
	private static List<String> method() {
		List<String> list = new LinkedList<String>();
		IntStream.range(0, 10000000).forEach(i -> list.add("a"));
		return list;
	}
	/**
	 * function
	 */
	private static Supplier<List<String>> function = () -> {
		List<String> list = new LinkedList<String>();
		IntStream.range(0, 10000000).forEach(v -> list.add("a"));
		return list;
	};
	/**
	 * -----method-----
	 * 1766534141ns
	 * 1802412812ns
	 * 1645580959ns
	 *
	 * -----function-----
	 * 1560679066ns
	 * 1584875301ns
	 * 1897649797ns
	 */
}

I really don't understand this It seems that it is useless if the usage of function is strange due to the load on memory What is function for? Is it really because of the intermediate operation of stream?

Well, is it the result that the method is good?

Recommended Posts

Old and new speed showdown
Processing speed with and without static
text as bitmap And New Line