java short,integer,long performance

前端 未结 6 716
我在风中等你
我在风中等你 2021-01-01 23:27

I read that JVM stores internally short, integer and long as 4 bytes. I read it from an article from the year 2000, so I don\'t know how true it is now.

For the newe

6条回答
  •  被撕碎了的回忆
    2021-01-01 23:55

    There's basically no difference. One has to "confuse" the JITC a bit so that it doesn't recognize that the increment/decrement operations are self-cancelling and that the results aren't used. Do that and the three cases come out about equal. (Actually, short seems to be a tiny bit faster.)

    public class ShortTest {
    
        public static void main(String[] args){
            // Do the inner method 5 times to see how it changes as the JITC attempts to
            // do further optimizations.
            for (int i = 0; i < 5; i++) {
                calculate(i);
            }
        }
    
        public static void calculate(int passNum){
    
            System.out.println("Pass " + passNum);
            // Broke into two (nested) loop counters so the total number of iterations could
            // be large enough to be seen on the clock.  (Though this isn't as important when
            // the JITC over-optimizations are prevented.)
            int M = 100000;
            int N = 100000;
            java.util.Random r = new java.util.Random();
            short x = (short) r.nextInt(1);
            short y1 = (short) (x + 1);
            int y2 = x + 1;
            long y3 = x + 1;
    
            long time1=System.currentTimeMillis();
            short s=x;
            for (int j = 0; j 100) {
                        System.out.println("Shouldn't be here");
                    }
                }
            }
            long time2=System.currentTimeMillis();
            System.out.println("Time elapsed for shorts: "+(time2-time1) + " (" + time1 + "," + time2 + ")");
    
    
            long time3=System.currentTimeMillis();
            int in=x;
            for (int j = 0; j 100) {
                        System.out.println("Shouldn't be here");
                    }
                }
            }
            long time4=System.currentTimeMillis();
            System.out.println("Time elapsed for ints: "+(time4-time3) + " (" + time3 + "," + time4 + ")");
    
    
            long time5=System.currentTimeMillis();
            long l=x;
            for (int j = 0; j 100) {
                        System.out.println("Shouldn't be here");
                    }
                }
            }
            long time6=System.currentTimeMillis();
            System.out.println("Time elapsed for longs: "+(time6-time5) + " (" + time5 + "," + time6 + ")");
    
    
            System.out.println(s+in+l);
        }
    }
    

    Results:

    C:\JavaTools>java ShortTest
    Pass 0
    Time elapsed for shorts: 59119 (1422405830404,1422405889523)
    Time elapsed for ints: 45810 (1422405889524,1422405935334)
    Time elapsed for longs: 47840 (1422405935335,1422405983175)
    0
    Pass 1
    Time elapsed for shorts: 58258 (1422405983176,1422406041434)
    Time elapsed for ints: 45607 (1422406041435,1422406087042)
    Time elapsed for longs: 46635 (1422406087043,1422406133678)
    0
    Pass 2
    Time elapsed for shorts: 31822 (1422406133679,1422406165501)
    Time elapsed for ints: 39663 (1422406165502,1422406205165)
    Time elapsed for longs: 37232 (1422406205165,1422406242397)
    0
    Pass 3
    Time elapsed for shorts: 30392 (1422406242398,1422406272790)
    Time elapsed for ints: 37949 (1422406272791,1422406310740)
    Time elapsed for longs: 37634 (1422406310741,1422406348375)
    0
    Pass 4
    Time elapsed for shorts: 31303 (1422406348376,1422406379679)
    Time elapsed for ints: 36583 (1422406379680,1422406416263)
    Time elapsed for longs: 38730 (1422406416264,1422406454994)
    0
    
    C:\JavaTools>java -version
    java version "1.7.0_65"
    Java(TM) SE Runtime Environment (build 1.7.0_65-b19)
    Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
    

提交回复
热议问题