http://pastebin.com/vRb4ptGf
Further performance gains can be achieved by using FastMap<ObjectAsKey, SomeValueNotNecessaryUsedForAnything> in other code fragments (eg. for keeping disabled skills in L2Character). You only have to make sure that ObjectAsKey has proper hashCode() method implementation. This is faster, than using eg. Integer as key. I've checked some time ago few most basic maps, lists etc, and from what I got, FastMap<ObjectAsKey, SomeValue> is fastest solution for iterating, adding, removing, getting non primitive objects. If you don't require objects to be contained in certain order, and you assume that there can be only one object copy in list, it's advantageous to even use FastMap over FastList or TIntObjHashMap.
Code: Select all
-------------------------------------------------FastList: new FastList<L2Character>().shared()FastList 2: new FastList<Integer>().shared()FastMap: new FastMap<Integer, L2Character>().shared()FastMap 2: new FastMap<L2Character, Integer>().shared()TIntObjHashMap: new TIntObjectHashMap<L2Character>()-------------------------------------------------FastList: 5908ms (size=50000) [if (!contains(cha)) add(cha)]FastMap: 55ms (size=50000) [if (!containsKey(cha.getObjectId())) put(cha.getObjectId(), cha)] Result -> FastList vs FastMap: 10742%-------------------------------------------------FastList 2: 9080ms (size=50000) [if (!contains(cha.getObjectId())) add(cha.getObjectId())]FastMap 2: 13ms (size=50000) [if (!containsKey(cha)) put(cha, cha.getObjectId())] Result -> FastList 2 vs FastMap 2: 69846%-------------------------------------------------FastList foreach: 19msFastMap 2 iterate keyset: 8ms Result -> FastList foreach vs FastMap 2 iterate keyset: 238%-------------------------------------------------FastMap values foreach: 8ms-------------------------------------------------TIntObjHashMap: 21ms (size=50000) [if (!containsKey(cha.getObjectId())) put(cha.getObjectId(), cha)]-------------------------------------------------TIntObjHashMap: 14454ms (size=50000) [if (!containsValue(cha)) put(cha.getObjectId(), cha)]
Code: Select all
package com.l2jbench; import gnu.trove.map.hash.TIntObjectHashMap;import javolution.util.FastList;import javolution.util.FastMap; import java.util.Iterator; public class Bench2{ public static class L2Character { private final int objectId; public L2Character(int objId) { objectId = objId; } public int getObjectId() { return objectId; } @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof L2Character)) return false; L2Character that = (L2Character) o; return objectId == that.getObjectId(); } @Override public int hashCode() { return objectId; } } public static void main(String[] args) { FastList<L2Character> fl = new FastList<L2Character>().shared(); FastList<Integer> fl2 = new FastList<Integer>().shared(); FastMap<Integer, L2Character> fm = new FastMap<Integer, L2Character>().shared(); FastMap<L2Character, Integer> fm2 = new FastMap<L2Character, Integer>().shared(); TIntObjectHashMap<L2Character> ioh = new TIntObjectHashMap<L2Character>(); long list = 0, map = 0; System.out.println("-------------------------------------------------"); System.out.println("FastList: new FastList<L2Character>().shared()"); System.out.println("FastList 2: new FastList<Integer>().shared()"); System.out.println("FastMap: new FastMap<Integer, L2Character>().shared()"); System.out.println("FastMap 2: new FastMap<L2Character, Integer>().shared()"); System.out.println("TIntObjHashMap: new TIntObjectHashMap<L2Character>()"); System.out.println("-------------------------------------------------"); long start = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { L2Character cha = new L2Character(i); if (!fl.contains(cha)) fl.add(cha); } long end = System.currentTimeMillis(); list = end - start; System.out.println("FastList: " + list + "ms (size=" + fl.size() + ") [if (!contains(cha)) add(cha)]"); start = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { L2Character cha = new L2Character(i); if (!fm.containsKey(cha.getObjectId())) fm.put(cha.getObjectId(), cha); } end = System.currentTimeMillis(); map = end - start; System.out.println("FastMap: " + map + "ms (size=" + fm.size() + ") [if (!containsKey(cha.getObjectId())) put(cha.getObjectId(), cha)]"); double result = (double)list * 100. / (double)map; System.out.println(" Result -> FastList vs FastMap: " + Math.round(result) + "%"); System.out.println("-------------------------------------------------"); start = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { L2Character cha = new L2Character(i); if (!fl2.contains(cha.getObjectId())) fl2.add(cha.getObjectId()); } end = System.currentTimeMillis(); list = end - start; System.out.println("FastList 2: " + list + "ms (size=" + fl2.size() + ") [if (!contains(cha.getObjectId())) add(cha.getObjectId())]"); start = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { L2Character cha = new L2Character(i); if (!fm2.containsKey(cha)) fm2.put(cha, cha.getObjectId()); } end = System.currentTimeMillis(); map = end - start; System.out.println("FastMap 2: " + map + "ms (size=" + fm2.size() + ") [if (!containsKey(cha)) put(cha, cha.getObjectId())]"); result = (double) list * 100. / (double) map; System.out.println(" Result -> FastList 2 vs FastMap 2: " + Math.round(result) + "%"); System.out.println("-------------------------------------------------"); start = System.currentTimeMillis(); for (L2Character cha : fl) { cha.getObjectId(); } end = System.currentTimeMillis(); list = end - start; System.out.println("FastList foreach: " + list + "ms"); start = System.currentTimeMillis(); Iterator<L2Character> it = fm2.keySet().iterator(); while (it.hasNext()) { L2Character cha = it.next(); cha.getObjectId(); } end = System.currentTimeMillis(); map = end - start; System.out.println("FastMap 2 iterate keyset: " + map + "ms"); result = (double) list * 100. / (double) map; System.out.println(" Result -> FastList foreach vs FastMap 2 iterate keyset: " + Math.round(result) + "%"); System.out.println("-------------------------------------------------"); start = System.currentTimeMillis(); for (L2Character cha : fm.values()) { cha.getObjectId(); } end = System.currentTimeMillis(); System.out.println("FastMap values foreach: " + (end - start) + "ms"); System.out.println("-------------------------------------------------"); start = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { L2Character cha = new L2Character(i); if (!ioh.containsKey(cha.getObjectId())) ioh.put(cha.getObjectId(), cha); } end = System.currentTimeMillis(); System.out.println("TIntObjHashMap: " + (end - start) + "ms (size=" + ioh.size() + ") [if (!containsKey(cha.getObjectId())) put(cha.getObjectId(), cha)]"); System.out.println("-------------------------------------------------"); ioh.clear(); start = System.currentTimeMillis(); for (int i = 0; i < 50000; i++) { L2Character cha = new L2Character(i); if (!ioh.containsValue(cha)) ioh.put(cha.getObjectId(), cha); } end = System.currentTimeMillis(); System.out.println("TIntObjHashMap: " + (end - start) + "ms (size=" + ioh.size() + ") [if (!containsValue(cha)) put(cha.getObjectId(), cha)]"); }}