[JAVA] Which is faster, size or 0, as the argument of List # toArray?

First conclusion

Background

→ Let's experiment!

Premise

Experimental code


import java.util.ArrayList;
import java.util.List;

public class Test {

	public static void main(String[] args) {

		List<String> list = new ArrayList<>();
		for (int i = 0; i < 1000; i++) {
			list.add(Integer.toString(i));
		}

		int[] loopCounts = new int[] { 1000, 10000, 100000, 1000000, 10000000 };
		for (int loop : loopCounts) {
			long sum = 0;
			for (int j = 0; j < 10; j++) {
				long start = System.currentTimeMillis();

				String[] temp = null;
				for (int i = 0; i < loop; i++) {
					temp = list.toArray(new String[0]);
				}

				long stop = System.currentTimeMillis();
				sum += stop - start;
			}
			System.out.println("toArray(new String[0]) " + loop + "Speed of times*The average speed of 10 times is" + sum / 10 + "It's a millisecond.");

			sum = 0;
			for (int j = 0; j < 10; j++) {
				long start = System.currentTimeMillis();

				String[] temp = null;
				for (int i = 0; i < loop; i++) {
					temp = list.toArray(new String[list.size()]);
				}

				long stop = System.currentTimeMillis();
				sum += stop - start;
			}
			System.out.println("toArray(new String[list.size()]) " + loop + "Speed of times*The average speed of 10 times is" + sum / 10 + "It's a millisecond.");
		}
	}
}

result

[1,000 times]

toArray (new String [0]) 1000 times speed * 10 times The average speed is 2 milliseconds.

toArray (new String [list.size ()]) 1000 times speed * 10 times The average speed is 2 milliseconds.

[10,000 times]

toArray (new String [0]) 10000 times speed * 10 times The average speed is 19 milliseconds.

toArray (new String [list.size ()]) 10000 times speed * 10 times The average speed is 25 milliseconds.

[100,000 times]

toArray (new String [0]) 100000 times speed * 10 times The average speed is 207 milliseconds.

toArray (new String [list.size ()]) 100000 times speed * 10 times The average speed is 288 milliseconds.

[1,000,000 times]

toArray (new String [0]) 1000000 times speed * 10 times The average speed is 1497 milliseconds.

toArray (new String [list.size ()]) 1000000 times speed * 10 times The average speed is 1562 milliseconds.

[10,000,000 times]

toArray (new String [0]) 10000000 times speed * 10 times The average speed is 12949 milliseconds.

toArray (new String [list.size ()]) 10000000 times speed * 10 times The average speed is 15622 milliseconds.

Conclusion again

** toArray (new T [0]) is faster! ** **

Afterword

It's crazy because it's written in an article that came out on the net (self-discipline)

URL that seems to be helpful

External link: Arrays of Wisdom of the Ancients

Recommended Posts

Which is faster, size or 0, as the argument of List # toArray?
Java: The problem of which is faster, stream or loop
The question of which is better, if or switch
Which is faster, method reference or lambda expression
Which is faster, Array # sample or Random # rand?
[Xcode / Swift5] Note "Expression following'return' is treated as an argument of the'return'", which is executed unintentionally after the return.
Which is better, Kotlin or Java in the future?
Is the method of the primitive specialized IntFunction apply or applyAsInt?
Change the conversation depending on which day of the week is today
[Java] Delete the elements of List
[Rails] Regarding the presence or absence of parentheses in the argument of the render method
[6 selections of form tool comparison] Which is better, open source or commercial?