我有一个用Java 8编写的相当简单的爱好项目,该项目在其操作模式之一中大量使用了重复的Math.round()调用。例如,一种这样的模式通过ExecutorService产生4个线程并将48个可运行任务排队,每个任务运行类似于以下代码块2 ^ 31次的操作:

int3 = Math.round(float1 + float2);
int3 = Math.round(float1 * float2);
int3 = Math.round(float1 / float2);

这并不完全是这样(涉及数组和嵌套循环),但是您知道了。无论如何,在Java 8u40之前,类似于以上代码的代码可以在AMD A10-7700k上约13秒内完成约1030亿个指令块的全部运行。使用Java 8u40,大约需要260秒才能完成相同的操作。无需更改代码,什么也没有,只是Java更新。

是否有其他人注意到Math.round()变慢得多,尤其是在重复使用它时?好像JVM在不进行任何优化之前就在进行某种优化。也许是8u40之前使用SIMD,现在不是吗?

编辑:我已经完成了对MVCE的第二次尝试。您可以在此处下载首次尝试:

https://www.dropbox.com/s/rm2ftcv8y6ye1bi/MathRoundMVCE.zip?dl=0

下面是第二次尝试。我的第一次尝试已被从这篇文章中删除,因为它被认为太长了,并且容易受到JVM死代码去除优化的影响(显然在8u40中这种情况很少发生)。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MathRoundMVCE
{
    static long grandtotal = 0;
    static long sumtotal = 0;

    static float[] float4 = new float[128];
    static float[] float5 = new float[128];
    static int[] int6 = new int[128];
    static int[] int7 = new int[128];
    static int[] int8 = new int[128];
    static long[] longarray = new long[480];

    final static int mil = 1000000;

    public static void main(String[] args)
    {
        initmainarrays();
        OmniCode omni = new OmniCode();
        grandtotal = omni.runloops() / mil;
        System.out.println("Total sum of operations is " + sumtotal);
        System.out.println("Total execution time is " + grandtotal + " milliseconds");
    }

    public static long siftarray(long[] larray)
    {
        long topnum = 0;
        long tempnum = 0;
        for (short i = 0; i < larray.length; i++)
        {
            tempnum = larray[i];
            if (tempnum > 0)
            {
                topnum += tempnum;
            }
        }
        topnum = topnum / Runtime.getRuntime().availableProcessors();
        return topnum;
    }

    public static void initmainarrays()
    {
        int k = 0;

        do
        {
            float4[k] = (float)(Math.random() * 12) + 1f;
            float5[k] = (float)(Math.random() * 12) + 1f;
            int6[k] = 0;

            k++;
        }
        while (k < 128);
    }
}

class OmniCode extends Thread
{
    volatile long totaltime = 0;
    final int standard = 16777216;
    final int warmup = 200000;

    byte threads = 0;

    public long runloops()
    {
        this.setPriority(MIN_PRIORITY);

        threads = (byte)Runtime.getRuntime().availableProcessors();
        ExecutorService executor = Executors.newFixedThreadPool(threads);

        for (short j = 0; j < 48; j++)
        {
            executor.execute(new RoundFloatToIntAlternate(warmup, (byte)j));
        }

        executor.shutdown();

        while (!executor.isTerminated())
        {
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                //Do nothing
            }
        }

        executor = Executors.newFixedThreadPool(threads);

        for (short j = 0; j < 48; j++)
        {
            executor.execute(new RoundFloatToIntAlternate(standard, (byte)j));
        }

        executor.shutdown();

        while (!executor.isTerminated())
        {
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                //Do nothing
            }
        }

        totaltime = MathRoundMVCE.siftarray(MathRoundMVCE.longarray);

        executor = null;
        Runtime.getRuntime().gc();
        return totaltime;
    }
}

class RoundFloatToIntAlternate extends Thread
{
    int i = 0;
    int j = 0;
    int int3 = 0;
    int iterations = 0;
    byte thread = 0;

    public RoundFloatToIntAlternate(int cycles, byte threadnumber)
    {
        iterations = cycles;
        thread = threadnumber;
    }

    public void run()
    {
        this.setPriority(9);
        MathRoundMVCE.longarray[this.thread] = 0;
        mainloop();
        blankloop();

    }

    public void blankloop()
    {
        j = 0;
        long timer = 0;
        long totaltimer = 0;

        do
        {
            timer = System.nanoTime();
            i = 0;

            do
            {
                i++;
            }
            while (i < 128);
            totaltimer += System.nanoTime() - timer;

            j++;
        }
        while (j < iterations);

        MathRoundMVCE.longarray[this.thread] -= totaltimer;
    }

    public void mainloop()
    {
        j = 0;
        long timer = 0;
        long totaltimer = 0;
        long localsum = 0;

        int[] int6 = new int[128];
        int[] int7 = new int[128];
        int[] int8 = new int[128];

        do
        {
            timer = System.nanoTime();
            i = 0;

            do
            {
                int6[i] = Math.round(MathRoundMVCE.float4[i] + MathRoundMVCE.float5[i]);
                int7[i] = Math.round(MathRoundMVCE.float4[i] * MathRoundMVCE.float5[i]);
                int8[i] = Math.round(MathRoundMVCE.float4[i] / MathRoundMVCE.float5[i]);

                i++;
            }
            while (i < 128);
            totaltimer += System.nanoTime() - timer;

            for(short z = 0; z < 128; z++)
            {
                localsum += int6[z] + int7[z] + int8[z];
            }

            j++;
        }
        while (j < iterations);

        MathRoundMVCE.longarray[this.thread] += totaltimer;
        MathRoundMVCE.sumtotal = localsum;
    }
}

长话短说,此代码在8u25中的执行效果与8u40中的大致相同。如您所见,我现在将所有计算的结果记录到数组中,然后将循环定时部分之外的那些数组求和到局部变量,然后在外部循环末尾将其写入静态变量。

在8u25以下:总执行时间为261545毫秒

在8u40以下:总执行时间为266890毫秒

测试条件与之前相同。因此,似乎8u25和8u31正在执行8u40停止执行的无效代码删除,导致代码在8u40中“变慢”。但这并不能解释所有奇怪的小事情,但似乎占了大部分。另外,这里提供的建议和答案给了我改善我的业余爱好项目其他部分的灵感,对此我深表感谢。谢谢大家!

最佳答案

基于OP的MVCE

  • 可能会进一步简化
  • int3 =语句更改为int3 +=,以减少死代码删除的机会。从8u31到8u40的int3 =差异慢了3倍。使用int3 +=的差异仅慢15%。
  • 打印结果可进一步减少死代码删除优化的机会


  • public class MathTime {
        static float[][] float1 = new float[8][16];
        static float[][] float2 = new float[8][16];
    
        public static void main(String[] args) {
            for (int j = 0; j < 8; j++) {
                for (int k = 0; k < 16; k++) {
                    float1[j][k] = (float) (j + k);
                    float2[j][k] = (float) (j + k);
                }
            }
            new Test().run();
        }
    
        private static class Test {
            int int3;
    
            public void run() {
                for (String test : new String[] { "warmup", "real" }) {
    
                    long t0 = System.nanoTime();
    
                    for (int count = 0; count < 1e7; count++) {
                        int i = count % 8;
                        int3 += Math.round(float1[i][0] + float2[i][0]);
                        int3 += Math.round(float1[i][1] + float2[i][1]);
                        int3 += Math.round(float1[i][2] + float2[i][2]);
                        int3 += Math.round(float1[i][3] + float2[i][3]);
                        int3 += Math.round(float1[i][4] + float2[i][4]);
                        int3 += Math.round(float1[i][5] + float2[i][5]);
                        int3 += Math.round(float1[i][6] + float2[i][6]);
                        int3 += Math.round(float1[i][7] + float2[i][7]);
                        int3 += Math.round(float1[i][8] + float2[i][8]);
                        int3 += Math.round(float1[i][9] + float2[i][9]);
                        int3 += Math.round(float1[i][10] + float2[i][10]);
                        int3 += Math.round(float1[i][11] + float2[i][11]);
                        int3 += Math.round(float1[i][12] + float2[i][12]);
                        int3 += Math.round(float1[i][13] + float2[i][13]);
                        int3 += Math.round(float1[i][14] + float2[i][14]);
                        int3 += Math.round(float1[i][15] + float2[i][15]);
    
                        int3 += Math.round(float1[i][0] * float2[i][0]);
                        int3 += Math.round(float1[i][1] * float2[i][1]);
                        int3 += Math.round(float1[i][2] * float2[i][2]);
                        int3 += Math.round(float1[i][3] * float2[i][3]);
                        int3 += Math.round(float1[i][4] * float2[i][4]);
                        int3 += Math.round(float1[i][5] * float2[i][5]);
                        int3 += Math.round(float1[i][6] * float2[i][6]);
                        int3 += Math.round(float1[i][7] * float2[i][7]);
                        int3 += Math.round(float1[i][8] * float2[i][8]);
                        int3 += Math.round(float1[i][9] * float2[i][9]);
                        int3 += Math.round(float1[i][10] * float2[i][10]);
                        int3 += Math.round(float1[i][11] * float2[i][11]);
                        int3 += Math.round(float1[i][12] * float2[i][12]);
                        int3 += Math.round(float1[i][13] * float2[i][13]);
                        int3 += Math.round(float1[i][14] * float2[i][14]);
                        int3 += Math.round(float1[i][15] * float2[i][15]);
    
                        int3 += Math.round(float1[i][0] / float2[i][0]);
                        int3 += Math.round(float1[i][1] / float2[i][1]);
                        int3 += Math.round(float1[i][2] / float2[i][2]);
                        int3 += Math.round(float1[i][3] / float2[i][3]);
                        int3 += Math.round(float1[i][4] / float2[i][4]);
                        int3 += Math.round(float1[i][5] / float2[i][5]);
                        int3 += Math.round(float1[i][6] / float2[i][6]);
                        int3 += Math.round(float1[i][7] / float2[i][7]);
                        int3 += Math.round(float1[i][8] / float2[i][8]);
                        int3 += Math.round(float1[i][9] / float2[i][9]);
                        int3 += Math.round(float1[i][10] / float2[i][10]);
                        int3 += Math.round(float1[i][11] / float2[i][11]);
                        int3 += Math.round(float1[i][12] / float2[i][12]);
                        int3 += Math.round(float1[i][13] / float2[i][13]);
                        int3 += Math.round(float1[i][14] / float2[i][14]);
                        int3 += Math.round(float1[i][15] / float2[i][15]);
    
                    }
                    long t1 = System.nanoTime();
                    System.out.println(int3);
                    System.out.println(String.format("%s, Math.round(float), %s, %.1f ms", System.getProperty("java.version"), test, (t1 - t0) / 1e6));
                }
            }
        }
    }
    

    结果
    adam@brimstone:~$ ./jdk1.8.0_40/bin/javac MathTime.java;./jdk1.8.0_40/bin/java -cp . MathTime
    1.8.0_40, Math.round(float), warmup, 6846.4 ms
    1.8.0_40, Math.round(float), real, 6058.6 ms
    adam@brimstone:~$ ./jdk1.8.0_31/bin/javac MathTime.java;./jdk1.8.0_31/bin/java -cp . MathTime
    1.8.0_31, Math.round(float), warmup, 5717.9 ms
    1.8.0_31, Math.round(float), real, 5282.7 ms
    adam@brimstone:~$ ./jdk1.8.0_25/bin/javac MathTime.java;./jdk1.8.0_25/bin/java -cp . MathTime
    1.8.0_25, Math.round(float), warmup, 5702.4 ms
    1.8.0_25, Math.round(float), real, 5262.2 ms
    

    观察结果
  • 对于Math.round(float)的微不足道的使用,我发现平台(Linux x86_64)的性能没有差异。基准之间只有一个区别,正如Ivan的答案和Marco13的评论所指出的那样,我以前的幼稚和不正确的基准仅暴露了优化行为的差异。
  • 8u40在消除死代码方面没有以前的版本那么积极,这意味着在某些特殊情况下执行更多代码,因此速度较慢。
  • 8u40需要稍长的时间来预热,但是一旦“出现”,就会更快。

  • 来源分析

    令人惊讶的是Math.round(float)是纯Java实现,而不是 native ,8u31和8u40的代码是相同的。
    diff  jdk1.8.0_31/src/java/lang/Math.java jdk1.8.0_40/src/java/lang/Math.java
    -no differences-
    
    public static int round(float a) {
        int intBits = Float.floatToRawIntBits(a);
        int biasedExp = (intBits & FloatConsts.EXP_BIT_MASK)
                >> (FloatConsts.SIGNIFICAND_WIDTH - 1);
        int shift = (FloatConsts.SIGNIFICAND_WIDTH - 2
                + FloatConsts.EXP_BIAS) - biasedExp;
        if ((shift & -32) == 0) { // shift >= 0 && shift < 32
            // a is a finite number such that pow(2,-32) <= ulp(a) < 1
            int r = ((intBits & FloatConsts.SIGNIF_BIT_MASK)
                    | (FloatConsts.SIGNIF_BIT_MASK + 1));
            if (intBits < 0) {
                r = -r;
            }
            // In the comments below each Java expression evaluates to the value
            // the corresponding mathematical expression:
            // (r) evaluates to a / ulp(a)
            // (r >> shift) evaluates to floor(a * 2)
            // ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
            // (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
            return ((r >> shift) + 1) >> 1;
        } else {
            // a is either
            // - a finite number with abs(a) < exp(2,FloatConsts.SIGNIFICAND_WIDTH-32) < 1/2
            // - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
            // - an infinity or NaN
            return (int) a;
        }
    }
    

    关于java - Java 8u40 Math.round()非常慢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29051488/

    10-10 07:44