Java – why are short primitive types significantly slower than long or int?

I try to optimize the RAM usage of Android games by changing the int primitive to short Before I did this, I was interested in the performance of primitive types in Java

So I created this little benchmark using the caliper library

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;
    }
}

The test results surprised me

testing environment

The benchmark runs under the caliper library

detection result

https://microbenchmarks.appspot.com/runs/0c9bd212-feeb-4f8f-896c-e027b85dfe3b

Int 2.365 ns

Length 2.436 NS

Short 8.156 NS

Test conclusion?

Short primitive types are much slower than long and int primitive types (3-4 ~)?

topic

>Why are short primitives significantly slower than int or long? I want the int primitive type to be the fastest on a 32-bit virtual machine, equal in length, or even faster. > Is the same true for Android phones? Knowing that Android phones usually run in 32 - bit environments, more and more phones are now equipped with 64 - bit processors

Solution

Java bytecode does not support basic operations (, –, *, /, > >, > >, < <,%) of basic types less than int No bytecode was allocated for this operation in the instruction set Therefore, VM needs to convert short (s) to int (s), perform the operation, and then truncate int to short and store it in the result Use javap to check the generated bytecode to see the difference between short and int tests VM / JIT optimizations obviously favor int / long operations, which makes sense because they are the most common Types less than int have their uses, but they are mainly used to save memory in arrays They are not as suitable as simple class members (of course, you will still use them when it applies to data types) Smaller members may not even reduce the size of the object At present, VM (again) is mainly customized for execution speed, so VM can even align fields with local machine word boundaries to improve access performance, but at the cost of memory cost

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>