我尝试通过将 int 原语更改为 short 来优化 Android 游戏的 RAM 使用情况。在我这样做之前,我对 Java 中原始类型的性能很感兴趣。
所以我使用 caliper 库创建了这个小测试基准。
public class BenchmarkTypes extends Benchmark {
@Param("10") private long testLong;
@Param("10") private int testInt;
@Param("10") private short testShort;
@Param("5000") private long resultLong = 5000;
@Param("5000") private int resultInt = 5000;
@Param("5000") private short resultShort = 5000;
@Override
protected void setUp() throws Exception {
Random rand = new Random();
testShort = (short) rand.nextInt(1000);
testInt = (int) testShort;
testLong = (long) testShort;
}
public long timeLong(int reps){
for(int i = 0; i < reps; i++){
resultLong += testLong;
resultLong -= testLong;
}
return resultLong;
}
public int timeInt(int reps){
for(int i = 0; i < reps; i++){
resultInt += testInt;
resultInt -= testInt;
}
return resultInt;
}
public short timeShort(int reps){
for(int i = 0; i < reps; i++){
resultShort += testShort;
resultShort -= testShort;
}
return resultShort;
}
}
测试结果让我吃惊。
测试环境
在 Caliper 库下运行基准测试。
测试结果
https://microbenchmarks.appspot.com/runs/0c9bd212-feeb-4f8f-896c-e027b85dfe3b
内部 2.365 纳秒
长 2.436 纳秒
8.156 ns 短
测试结论?
short 原始类型比 long 和 int 原始类型慢得多(3-4~ 倍)?
问题
为什么 short 原语比 int 或 long 慢得多?我希望 int 原始类型在 32 位 VM 上是最快的,并且 long 和 short 在时间上相等,或者 short 更快。
Android手机也是这样吗?知道 Android 手机通常在 32 位环境中运行,现在越来越多的手机开始配备 64 位处理器。
Best Answer-推荐答案
Java 字节码不支持对小于 int 的基本类型的基本操作(+、-、*、/、>>、>>>、<<、%)。在指令集中根本没有为此类操作分配字节码。因此,VM 需要将 short(s) 转换为 int(s),执行操作,然后将 int 截断回 short 并将其存储在结果中。
用 javap 检查生成的字节码,看看你的 short 和 int 测试之间的区别。
VM/JIT 优化显然严重偏向 int/long 操作,这是有道理的,因为它们是最常见的。
小于 int 的类型有其用途,但主要用于在数组中节省内存。它们不像简单的类成员那样适合(当然,当它是数据的适当类型时,您仍然会使用它们)。较小的成员可能甚至不会减小对象的大小。当前的虚拟机(再次)主要针对执行速度量身定制,因此虚拟机甚至可以将字段与 native 机器字边界对齐,以牺牲内存消耗来提高访问性能。
关于java - 为什么 short 基本类型比 long 或 int 慢得多?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/24304433/
|