相关疑难解决方法(0)

JIT没有优化涉及Integer.MAX_VALUE的循环

在写另一个问题的答案时,我注意到JIT优化的一个奇怪的边界情况.

以下程序不是 "Microbenchmark",也不是为了可靠地测量执行时间(如另一个问题的答案中所指出的).它仅用作MCVE来重现该问题:

class MissedLoopOptimization
{
    public static void main(String args[])
    {
        for (int j=0; j<3; j++)
        {
            for (int i=0; i<5; i++)
            {
                long before = System.nanoTime();
                runWithMaxValue();
                long after = System.nanoTime();
                System.out.println("With MAX_VALUE   : "+(after-before)/1e6);
            }
            for (int i=0; i<5; i++)
            {
                long before = System.nanoTime();
                runWithMaxValueMinusOne();
                long after = System.nanoTime();
                System.out.println("With MAX_VALUE-1 : "+(after-before)/1e6);
            }
        }
    }

    private static void runWithMaxValue()
    {
        final int n = Integer.MAX_VALUE;
        int i = …
Run Code Online (Sandbox Code Playgroud)

java jit jvm-hotspot compiler-optimization

49
推荐指数
1
解决办法
1394
查看次数

标签 统计

compiler-optimization ×1

java ×1

jit ×1

jvm-hotspot ×1