I tried performance test to determine which to accept as an answer and got a surprising result.
Exexution time gap gets smaller as I execute the test again.
Test code:
Main:
import java.util.*; public class Main { public static void main(String[] args) { long time; long lapse1,lapse2; time=System.currentTimeMillis(); //lapse1=time; //System.out.println("time"+time); for(int i=0;i<15;++i) System.out.println(ARMConstants.Condition.nameForCode(i)); lapse1=System.currentTimeMillis()-time; time=System.currentTimeMillis(); //System.out.println("lapse1"+System.currentTimeMillis()); for(int i=0;i<15;++i) System.out.println(MapConst.getCCName(i)); lapse2=System.currentTimeMillis()-time; //System.out.println("time"+System.currentTimeMillis()); System.out.println("lapse1: "+lapse1+"lapse2:"+lapse2); } }
ARMConstants:
public class ARMConstants { private ARMConstants() {} public static enum Condition { INVALID, EQ, NE, HS, LO, MI, PL, VS, VC, HI, LS, GE, LT, GT, LE, AL; public int code() { return this.ordinal(); } public static String nameForCode(int cc) { try { return values()[cc].name(); } catch (ArrayIndexOutOfBoundsException noSuchCode) { // Log.e("Disassembler", "", noSuchCode); return ""; } } } }
MapConst:
import java.lang.reflect.*; import java.util.*; public class MapConst { private static final Map<Integer, String> _int2string; public static final int ARM_CC_INVALID = 0; public static final int ARM_CC_EQ = 1; public static final int ARM_CC_NE = 2; public static final int ARM_CC_HS = 3; public static final int ARM_CC_LO = 4; public static final int ARM_CC_MI = 5; public static final int ARM_CC_PL = 6; public static final int ARM_CC_VS = 7; public static final int ARM_CC_VC = 8; public static final int ARM_CC_HI = 9; public static final int ARM_CC_LS = 10; public static final int ARM_CC_GE = 11; public static final int ARM_CC_LT = 12; public static final int ARM_CC_GT = 13; public static final int ARM_CC_LE = 14; public static final int ARM_CC_AL = 15; static { final Map<Integer, String> int2string = new HashMap<>(); try { for (Field field: MapConst.class.getFields()) { final int mod = field.getModifiers(); if (!int.class.equals(field.getType())) continue; if (!Modifier.isStatic(mod) || !Modifier.isPublic(mod)) continue; if (!field.getName().startsWith("ARM_CC_")) continue; int2string.put(field.getInt(null), field.getName().substring("ARM_CC_".length())); } } catch (IllegalAccessException l_e) { throw new RuntimeException(l_e); // should not occur } _int2string = Collections.unmodifiableMap(int2string); } public static String getCCName(int cc) { return _int2string.get(cc); } }
Result:



Odd!
And more odd thing happened!
I added my code.
for(int i=0;i<15;++i) System.out.println(MapConst.getCCNameMine(i)); lapse3=System.currentTimeMillis()-time; //System.out.println("time"+System.currentTimeMillis()); System.out.println("lapse1: "+lapse1+"lapse2:"+lapse2+"lapse3:"+lapse3); public static String getCCNameMine(int cc) { Class clazz=MapConst.class; Field[] fields=clazz.getFields(); for(Field f:fields) { String s=f.getName(); if(s.contains("ARM_CC_")) try { if (((int)f.get(null))==cc) { return s.replace("ARM_CC_",""); } } catch (IllegalAccessException e) { //Log.e("Disassembler","",e); } catch (IllegalArgumentException e) { //Log.e("Disassembler","",e); } } return ""; }

Mine is fastest...
Edit
When I change System. currentTimeMillis to nanoTime:
lapse1: 5432129lapse2:3326416lapse3:9307861
So using enum is fastest.
Nono, I didn't check working performance nicely.
I may have test a lot more times;;
100 iteration:
lapse1: 293518068 lapse2:281768798 lapse3:433898927