Tbpgr Blog

Ruby プログラマ tbpgr(てぃーびー) のブログ

HashMapとEnumMapの処理時間を比較

パンくず

Java
HashMapとEnumMapの比較

概要

HashMapとEnumMapの比較

詳細

HashMapとEnumMapの性能を比較します。

比較時の使用はHashMapとEnumMapそれぞれに100個のEnumをキーに設定。
Valueには100個のIntegerを設定します。
設定後に、100個のEnumで100回Mapから値を取得して文字列として標準出力します。
この時間をナノ秒で比較します。

サンプル・コード

package compare_enum_map;

import java.util.Arrays;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CompareEnumMap {
  private static List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
      19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46,
      47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
      75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100);

  private static List<Hoge> hogeList = Arrays.asList(Hoge.VARIABLE_001, Hoge.VARIABLE_002, Hoge.VARIABLE_003,
      Hoge.VARIABLE_004, Hoge.VARIABLE_005, Hoge.VARIABLE_006, Hoge.VARIABLE_007, Hoge.VARIABLE_008, Hoge.VARIABLE_009,
      Hoge.VARIABLE_010, Hoge.VARIABLE_011, Hoge.VARIABLE_012, Hoge.VARIABLE_013, Hoge.VARIABLE_014, Hoge.VARIABLE_015,
      Hoge.VARIABLE_016, Hoge.VARIABLE_017, Hoge.VARIABLE_018, Hoge.VARIABLE_019, Hoge.VARIABLE_020, Hoge.VARIABLE_021,
      Hoge.VARIABLE_022, Hoge.VARIABLE_023, Hoge.VARIABLE_024, Hoge.VARIABLE_025, Hoge.VARIABLE_026, Hoge.VARIABLE_027,
      Hoge.VARIABLE_028, Hoge.VARIABLE_029, Hoge.VARIABLE_030, Hoge.VARIABLE_031, Hoge.VARIABLE_032, Hoge.VARIABLE_033,
      Hoge.VARIABLE_034, Hoge.VARIABLE_035, Hoge.VARIABLE_036, Hoge.VARIABLE_037, Hoge.VARIABLE_038, Hoge.VARIABLE_039,
      Hoge.VARIABLE_040, Hoge.VARIABLE_041, Hoge.VARIABLE_042, Hoge.VARIABLE_043, Hoge.VARIABLE_044, Hoge.VARIABLE_045,
      Hoge.VARIABLE_046, Hoge.VARIABLE_047, Hoge.VARIABLE_048, Hoge.VARIABLE_049, Hoge.VARIABLE_050, Hoge.VARIABLE_051,
      Hoge.VARIABLE_052, Hoge.VARIABLE_053, Hoge.VARIABLE_054, Hoge.VARIABLE_055, Hoge.VARIABLE_056, Hoge.VARIABLE_057,
      Hoge.VARIABLE_058, Hoge.VARIABLE_059, Hoge.VARIABLE_060, Hoge.VARIABLE_061, Hoge.VARIABLE_062, Hoge.VARIABLE_063,
      Hoge.VARIABLE_064, Hoge.VARIABLE_065, Hoge.VARIABLE_066, Hoge.VARIABLE_067, Hoge.VARIABLE_068, Hoge.VARIABLE_069,
      Hoge.VARIABLE_070, Hoge.VARIABLE_071, Hoge.VARIABLE_072, Hoge.VARIABLE_073, Hoge.VARIABLE_074, Hoge.VARIABLE_075,
      Hoge.VARIABLE_076, Hoge.VARIABLE_077, Hoge.VARIABLE_078, Hoge.VARIABLE_079, Hoge.VARIABLE_080, Hoge.VARIABLE_081,
      Hoge.VARIABLE_082, Hoge.VARIABLE_083, Hoge.VARIABLE_084, Hoge.VARIABLE_085, Hoge.VARIABLE_086, Hoge.VARIABLE_087,
      Hoge.VARIABLE_088, Hoge.VARIABLE_089, Hoge.VARIABLE_090, Hoge.VARIABLE_091, Hoge.VARIABLE_092, Hoge.VARIABLE_093,
      Hoge.VARIABLE_094, Hoge.VARIABLE_095, Hoge.VARIABLE_096, Hoge.VARIABLE_097, Hoge.VARIABLE_098, Hoge.VARIABLE_099,
      Hoge.VARIABLE_100);

  private enum Hoge {
    VARIABLE_001, VARIABLE_002, VARIABLE_003, VARIABLE_004, VARIABLE_005, VARIABLE_006, VARIABLE_007, VARIABLE_008, VARIABLE_009, VARIABLE_010,
    VARIABLE_011, VARIABLE_012, VARIABLE_013, VARIABLE_014, VARIABLE_015, VARIABLE_016, VARIABLE_017, VARIABLE_018, VARIABLE_019, VARIABLE_020,
    VARIABLE_021, VARIABLE_022, VARIABLE_023, VARIABLE_024, VARIABLE_025, VARIABLE_026, VARIABLE_027, VARIABLE_028, VARIABLE_029, VARIABLE_030,
    VARIABLE_031, VARIABLE_032, VARIABLE_033, VARIABLE_034, VARIABLE_035, VARIABLE_036, VARIABLE_037, VARIABLE_038, VARIABLE_039, VARIABLE_040,
    VARIABLE_041, VARIABLE_042, VARIABLE_043, VARIABLE_044, VARIABLE_045, VARIABLE_046, VARIABLE_047, VARIABLE_048, VARIABLE_049, VARIABLE_050,
    VARIABLE_051, VARIABLE_052, VARIABLE_053, VARIABLE_054, VARIABLE_055, VARIABLE_056, VARIABLE_057, VARIABLE_058, VARIABLE_059, VARIABLE_060,
    VARIABLE_061, VARIABLE_062, VARIABLE_063, VARIABLE_064, VARIABLE_065, VARIABLE_066, VARIABLE_067, VARIABLE_068, VARIABLE_069, VARIABLE_070,
    VARIABLE_071, VARIABLE_072, VARIABLE_073, VARIABLE_074, VARIABLE_075, VARIABLE_076, VARIABLE_077, VARIABLE_078, VARIABLE_079, VARIABLE_080,
    VARIABLE_081, VARIABLE_082, VARIABLE_083, VARIABLE_084, VARIABLE_085, VARIABLE_086, VARIABLE_087, VARIABLE_088, VARIABLE_089, VARIABLE_090,
    VARIABLE_091, VARIABLE_092, VARIABLE_093, VARIABLE_094, VARIABLE_095, VARIABLE_096, VARIABLE_097, VARIABLE_098, VARIABLE_099, VARIABLE_100,
  }

  public static void main(String[] args) {

    Map<Hoge, Integer> hashMap = new HashMap<Hoge, Integer>();
    long start = System.nanoTime();
    setMap(hashMap);
    printHogeList(hashMap);
    long end = System.nanoTime();
    System.out.println(end - start);

    Map<Hoge, Integer> enumMap = new EnumMap<Hoge, Integer>(Hoge.class);
    long start_ = System.nanoTime();
    setMap(enumMap);
    printHogeList(enumMap);
    long end_ = System.nanoTime();
    System.out.println(end_ - start_);

  }

  private static void printHogeList(Map<Hoge, Integer> hashMap) {
    for (Hoge hoge : hogeList) {
      System.out.print(hashMap.get(hoge));
    }
    System.out.println();
  }

  private static void setMap(Map<Hoge, Integer> hashMap) {
    int i = 0;
    for (Hoge hoge : Hoge.values()) {
      hashMap.put(hoge, intList.get(i));
      i++;
    }
  }
}

計測結果

HashMap EnumMap
1306126 1066151
1329671 1063736
2296511 1898666
1379174 1198062
1448300 1185987
1359856 1086375

結果

ナノ秒単位かつ計測時のデータ件数がそれほどでもないため
大差はありませんが安定して計測時間に差が出ています。
わざわざ遅い方を利用する理由はないため、Enumがキーの場合は必ずEnumMapを使うこと。