开发者

How expensive it is to cast from int to short in Java

In terms of runtime performance, how expensive it is to cast int to short in Java? There may be thousands of such c开发者_JS百科asting, hence I wonder if it would impact the performance or not. Thanks.


No. It won't impact performances. It is a single simple operation. When you want to analize performances of a software you better focus on the computational cost of algorithmic operations based on the size of input.


Leaving philosophical arguments and excuses aside ...

First of all short is a special data type. Java doesn't really like short. Java actually really loves int because the JVM stack uses 32 bit registers. And a short is a 16-bit datatype (int = 32-bit).

Because of the 32-bit structure, whenever java moves a short to the stack it is automatically converted to an integer. So, the first thing to wonder about is really, do I want to use short's at all in java ? They come indeed with a cost. That's why you will rarely ever see any short datatype usages in jdk sourcecode.

The JVM uses the i2s operation when converting an integer to a short. The exact cost will depend on what JVM you are using and your hardware.

You can find some of the stats in this paper, But the i2s is not listed unfortunately. It should take less than 20ns though.


You can neglect the cost of that cast. You won't notice thousands of such casts.


I think the safe practice is to not worry about performance until you have a performance problem. And when you do have a performance problem, it's extremely likely that in most business applications the majority of an applications sluggishness can be accounted for in its interactions with the disk and/or network. I think it's very unlikely that micro optimizations like this will have much of an impact on your performance.


Why do you need to do this? I do not think that it would effect performance that much, but keep in mind the range of the data type you need:

int:   -2,147,483,648 to 2,147,483,647
short: -32,768 to 32,767


The cast is small compared with loading a int from memory or storing a short. In any case they all cost about 2 nano-second. If you do thousands of these it will cost a few micro-seconds.


Below is a test for the long case, but can easily be adapted to short. Casting the int to a long is in this example about 5% slower compared to just passing in the long.

Interestingly calling the method using an implicit cast is slower thank casting it yourself.

With no cast : PT0.141517096S With cast : PT0.148024511S With implicit cast: PT0.159904349S

@Test
public void testPerformance(){
    long sum =0L;
    long timeCallWithImplicitCast =0;
    long timeCallWithLong =0;
    long timeCallWithCast =0;


    for(int j=0;j<2;j++) {//First run warm-up
        timeCallWithCast=0;
        timeCallWithLong=0;
        timeCallWithImplicitCast=0;
        for (int i = 0; i < 10_000_000; i++) {
            long s1 = System.nanoTime();
            sum += shift(i);//Call with int implicit cast
            long e1 = System.nanoTime();
            timeCallWithImplicitCast += (e1 - s1);
        }
        for (int i = 0; i < 10_000_000; i++) {
            long s3 = System.nanoTime();
            sum += shift((long) i);//Call with cast long
            long e3 = System.nanoTime();
            timeCallWithCast += (e3 - s3);
        }
        for (int i = 0; i < 10_000_000; i++) {
            long l = (long) i;
            long s2 = System.nanoTime();
            sum += shift(l);//Call with long
            long e2 = System.nanoTime();
            timeCallWithLong += (e2 - s2);
        }
    }
    System.out.println("With no cast      : "+ Duration.ofNanos(timeCallWithLong));
    System.out.println("With cast         : "+Duration.ofNanos(timeCallWithCast));
    System.out.println("With implicit cast: "+Duration.ofNanos(timeCallWithImplicitCast));

}

protected long shift(long index){
    return index << 4;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜