Why is long slower than int in x64 Java?

前端 未结 8 902
野性不改
野性不改 2020-12-04 09:10

I\'m running Windows 8.1 x64 with Java 7 update 45 x64 (no 32 bit Java installed) on a Surface Pro 2 tablet.

The code below takes 1688ms when the type of i is a long

8条回答
  •  清歌不尽
    2020-12-04 09:26

    I have just written a benchmark using caliper.

    The results are quite consistent with the original code: a ~12x speedup for using int over long. It certainly seems that the loop unrolling reported by tmyklebu or something very similar is going on.

    timeIntDecrements         195,266,845.000
    timeLongDecrements      2,321,447,978.000
    

    This is my code; note that it uses a freshly-built snapshot of caliper, since I could not figure out how to code against their existing beta release.

    package test;
    
    import com.google.caliper.Benchmark;
    import com.google.caliper.Param;
    
    public final class App {
    
        @Param({""+1}) int number;
    
        private static class IntTest {
            public static int v;
            public static void reset() {
                v = Integer.MAX_VALUE;
            }
            public static boolean decrementAndCheck() {
                return --v < 0;
            }
        }
    
        private static class LongTest {
            public static long v;
            public static void reset() {
                v = Integer.MAX_VALUE;
            }
            public static boolean decrementAndCheck() {
                return --v < 0;
            }
        }
    
        @Benchmark
        int timeLongDecrements(int reps) {
            int k=0;
            for (int i=0; i

提交回复
热议问题