为什么java代码的第二部分总是需要相当多的时间来运行?

时间:2015-08-30 12:36:27

标签: java benchmarking

当我在问this问题时尝试按照建议进行基准测试时,我发现无论如何,我测试的第二段代码都显着延长了1000 ns。我甚至交换了被测试的代码片段,看看是否真的只是比另一个长,并且交换时间越长意味着代码中的位置会影响它运行的时间。这是我的基准代码:

public class JavaAsmTest
{
    public static void main(String [] args)
    {
        int numOfTrials = 10;

        long [] codeATimes = new long [numOfTrials];
        long [] codeBTimes = new long [numOfTrials];
        for(int trial = 0; trial < numOfTrials; trial++)
        {
            {
                long startTime;
                long endTime;
                long deltaTime;
                startTime = System.nanoTime();
                for(int x = 0; x < 0x10000; x++)
                {
                    for(int y = 0; y < 0x10000; y++)
                    {
                        codeB(x, y); //the code being tested
                    }//end y loop
                }//end x loop
                endTime = System.nanoTime();
                deltaTime = endTime - startTime;
                codeBTimes[trial] = deltaTime;
            }//end codeB Trial
            {
                long startTime;
                long endTime;
                long deltaTime;
                startTime = System.nanoTime();
                for(int x = 0; x < 0x10000; x++)
                {
                    for(int y = 0; y < 0x10000; y++)
                    {
                        codeA(x, y); //the other code being tested
                    }//end y loop
                }//end x loop
                endTime = System.nanoTime();
                deltaTime = endTime - startTime;
                codeATimes[trial] = deltaTime;
            }//end codeA Trial

        }//end trial loop

        long codeASum = 0;
        long codeBSum = 0;

        for(int x = 0; x < numOfTrials; x++)
        {
            codeASum += codeATimes[x];
            codeBSum += codeBTimes[x];
        }

        long codeAAvg = codeASum / numOfTrials;
        long codeBAvg = codeBSum / numOfTrials;

        System.out.println("codeA avg: " + codeAAvg);
        System.out.println("codeB avg: " + codeBAvg);

    }//end main

    private static void codeA(int a, int b)
    {
        int result = (a + b) & 0xFFFF;

        if(result == 0)
        {
            setZeroFlag(true);
        }
        else
        {
            setZeroFlag(false);
        }
        setSubtFlag(false);
        if((((a & 0x0FFF) + (b & 0x0FFF)) & 0x1000) != 0)
        {
            setHCarryFlag(true);
        }
        else
        {
            setHCarryFlag(false);
        }
    }

    private static void codeB(int a, int b)
    {
        int result = (a + b) & 0xFFFF;

        if(result == 0)
        {
            setZeroFlag(true);
        }
        else
        {
            setZeroFlag(false);
        }
        setSubtFlag(false);

        setHCarryFlag((((a & 0x0FFF) + (b & 0x0FFF)) & 0x1000) != 0);
    }

    private static void setZeroFlag(boolean flag)
    {
    }
    private static void setHCarryFlag(boolean flag)
    {
    }
    private static void setSubtFlag(boolean flag)
    {
    }
}

请解释为什么会发生这种情况以及如何防止它给我一个现实的基准。

0 个答案:

没有答案