2个几乎相同的java方法之间的性能差异为2:1

时间:2014-12-26 18:03:45

标签: java jit

我的问题是,从比我更了解JIT的人,为什么在方法calculate2和calculate3之间执行时间之间存在如此大的差异(2:1比率),其中唯一的区别是calculate2()设置了一个字段结果和calculate3()返回它。

我有兴趣了解这一点,因为我在很多应用程序中非常依赖高速处理,这对性能影响很大。在这种特殊情况下,calculate1()中的本机代码仍比2:1优于最佳优化JIT代码。

以下是我的基准测试结果:

CALCULATE1:: 156ms: calculated = 499,999,999,500,000,000
CALCULATE2:: 270ms: calculated = 499,999,999,500,000,000
CALCULATE3:: 411ms: calculated = 499,999,999,500,000,000

系统信息:

Linux tardis 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

以下是生成此代码的代码:

package org.cni.examples;

import org.cni.annotation.CAccessor;

public class SumIntegers {

    public static void main(String[] args) {
        System.loadLibrary("HelloWorld");

        long max = (args.length == 1 ? Long.parseLong(args[0]) : 1000000000L);

        SumIntegers worker = new SumIntegers();

        long ts = System.currentTimeMillis();
        worker.calculate1(max);
        long te = System.currentTimeMillis();

        System.out.printf("CALCULATE1:: %dms: calculated = %,d%n", (te - ts),
                worker.calculated);

        ts = System.currentTimeMillis();
        worker.calculated = worker.calculate2(max);
        te = System.currentTimeMillis();

        System.out.printf("CALCULATE2:: %dms: calculated = %,d%n", (te - ts),
                worker.calculated);


        ts = System.currentTimeMillis();
        worker.calculate3(max);
        te = System.currentTimeMillis();

        System.out.printf("CALCULATE3:: %dms: calculated = %,d%n", (te - ts),
                worker.calculated);
    }

    @CAccessor private long calculated;

    public native void calculate1(long max)
    /*+{
        jlong sum = 0;
        for (jlong i = 0; i < max; i ++) {
            sum += i;
        }

        // Accessor for java field: calculated  
        SumIntegers_setField_calculated(env, self_arg, sum);
    }*/;

    public long calculate2(long max) {
        long sum = 0;
        for (long i = 0; i < max; i++) {
            sum += i;
        }

        return sum; // The only difference between 3
    }

    public void calculate3(long max) {
        long sum = 0;
        for (long i = 0; i < max; i++) {
            sum += i;
        }

        this.calculated = sum; // The only difference between 2
    }
}

0 个答案:

没有答案