0
0

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 3 years have passed since last update.

配列 vs ArrayList vs HashMapの拡張for文レース

Last updated at Posted at 2020-02-12

訂正

比較演算子間違ってました(修正済み)

つかったもの

・Java9
・eclipce

動機

何が最適かは置いといて拡張for文で回したらどれが一番早いのかなと。

結果

ForEaerVs.java
public class ForEaerVs {
	public static void main(String[] argS) {

		Integer mm;
		
		// 配列
		Integer[] arry = new Integer[1000000000];

		for (Integer i = 0 ; i < 1000000000; i++) {
			Integer arryInt = i;
			arry[i] = arryInt;
		}
		long staetTimeArry = System.nanoTime();
			for(Integer iA : arry) {
				mm = iA;
			}
		long endTimeArry = System.nanoTime();
		System.out.println("配列速度:" + (endTimeArry -staetTimeArry));

		// List
		List<Integer> list = new ArrayList<Integer>();
		for (Integer j = 0 ; j < 1000000000; j++) {
			Integer arryInt = j;
			list.add(arryInt);
		}
		long staetTimeList = System.nanoTime();
			for (Integer iL: list) {
				mm = iL;
			}
		long endTimeList = System.nanoTime();
		System.out.println("List速度:" + (endTimeList -staetTimeList));

		
		// HushMap
		HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();
		for (Integer k = 0 ; k < 1000000000; k++) {
			Integer arryInt = k;
			hash.put(k, arryInt);
		}
		long staetTimeHash = System.nanoTime();
			for (Integer iH : hash.values()) {
				mm = iH;
			}
		long endTimeHash = System.nanoTime();
		System.out.println("Hash速度:" + (endTimeHash -staetTimeHash));

	}
}
ForEaerVsResult.java
配列速度  4020600
List速度  11350400
Hash速度  18974400

作成時間もいれた結果は以下

ForEaerVsPlusCreateResult.java
配列速度 35613700
List速度 98473300
Hash速度 121742700

感想

それぞれ使いどころは違うとはいえ、配列ってこんなに遅いのか。
配列先輩の凄さを崇めよ

追加

LinkedListでもやってみた

ForEaerVsResult.java
Array速度 4001200
List速度  9521900
Hash速度  19611200
Linked速度:15211300

作成時間もいれた結果は以下

ForEaerVsPlusCreateResult.java
Array速度 32542100
List速度  80343600
Hash速度  101256200
Linked速度:140576800
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?