Mig*_*ork 80 java switch-statement
我试过谷歌这个,但没有运气.
我有一个非常大的转变,有些情况显然比其他情况更常见.
因此,我想知道订单是否真的保持不变,"上"案例在"较低"之前进行测试,因此评估得更快.
我想保留我的订单,但如果它伤害速度,那么重新排序分支将是一个好主意.
例如:
switch (mark) {
case Ion.NULL:
return null;
case Ion.BOOLEAN:
return readBoolean();
case Ion.BYTE:
return readByte();
case Ion.CHAR:
return readChar();
case Ion.SHORT:
return readShort();
case Ion.INT:
return readInt();
case Ion.LONG:
return readLong();
case Ion.FLOAT:
return readFloat();
case Ion.DOUBLE:
return readDouble();
case Ion.STRING:
return readString();
case Ion.BOOLEAN_ARRAY:
return readBooleans();
case Ion.BYTE_ARRAY:
return readBytes();
case Ion.CHAR_ARRAY:
return readChars();
case Ion.SHORT_ARRAY:
return readShorts();
case Ion.INT_ARRAY:
return readInts();
case Ion.LONG_ARRAY:
return readLongs();
case Ion.FLOAT_ARRAY:
return readFloats();
case Ion.DOUBLE_ARRAY:
return readDoubles();
case Ion.STRING_ARRAY:
return readStrings();
default:
throw new CorruptedDataException("Invalid mark: " + mark);
}
Run Code Online (Sandbox Code Playgroud)
Lou*_*man 112
重新排序switch语句没有任何影响.
查看Java字节码规范,switch可以将a编译为a lookupswitch或tableswitch指令,然后打开int.A lookupswitch总是以排序的顺序编译可能的值,因此重新排序代码中的常量永远不会重要,并且tableswitch只有一个相对于指定偏移量的可能跳转数组,因此它也不关心原始顺序.
请参阅http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.lookupswitch和http://docs.oracle.com/javase/specs/jvms/se7 /html/jvms-6.html#jvms-6.5.tableswitch了解详情.
| 归档时间: |
|
| 查看次数: |
4987 次 |
| 最近记录: |