为什么一个小的包装方法未得到优化

  • 本文关键字:方法 包装 优化 一个 java
  • 更新时间 :
  • 英文 :


我正在运行一个实验,以确定包装方法的性能开销。我读到JIT编译器和/或JVM优化了小方法,但我似乎一直遭受3-5%的性能罚款。

代码如下:

import java.util.* ;
public class WrappingTest1{
    private WrappingTest1(){
        // Empty.
    }
    private static void findPrimes(
        final Long maxValue ,
        final List< Long > foundPrimes
    ){
        if(
            maxValue > 2L
        ){
            Boolean isPrime ;
            foundPrimes.clear() ;
            for(
                Long i = 2L ;
                i <= maxValue ;
                i += 1L
            ){
                isPrime = true ;
                for(
                    Long j = 2L ;
                    j < i ;
                    j += 1L
                ){
                    if(
                        ( i % j ) == 0
                    ){
                        isPrime = false ;
                    }
                }
                if(
                    isPrime
                ){
                    foundPrimes.add(
                        i
                    ) ;
                }
            }
        }
    }
    private static void wrapper(
        final Long input ,
        final List< Long > output
    ){
        findPrimes(
            input ,
            output
        ) ;
    }
    public static void main(
        final String[] args
    ){
        ArrayList< Long > primes ;
        Long startTime ;
        Long endTime ;
        Double duration ;
        Double meanDuration ;
        Long primeRange ;
        Long warmupIterations ;
        Long benchmarkIterations ;
        primes = new ArrayList<>() ;
        meanDuration = 0.0 ;
        primeRange = 100L ;
        warmupIterations = 20000L ;
        benchmarkIterations = 100000L ;
        System.out.println(
            "Experiment started."
        ) ;
        // Unwrapped warmup.
        for(
            Long i = 0L ;
            i < warmupIterations ;
            i += 1L
        ){
            findPrimes(
                primeRange ,
                primes
            ) ;
        }
        // Unwrapped benchmark.
        startTime = System.nanoTime() ;
        for(
            Long i = 0L ;
            i < benchmarkIterations ;
            i += 1L
        ){
            findPrimes(
                primeRange ,
                primes
            ) ;
        }
        endTime = System.nanoTime() ;
        duration = ( endTime.doubleValue() - startTime.doubleValue() ) / 1E9 ;
        System.out.println(
            "Unwrapped runtime: " + duration + " seconds."
        ) ;
        // Wrapped warmup.
        for(
            Long i = 0L ;
            i < warmupIterations ;
            i += 1L
        ){
            wrapper(
                primeRange ,
                primes
            ) ;
        }
        // Wrapped benchmark.
        startTime = System.nanoTime() ;
        for(
            Long i = 0L ;
            i < benchmarkIterations ;
            i += 1L
        ){
            wrapper(
                primeRange ,
                primes
            ) ;
        }
        endTime = System.nanoTime() ;
        duration = ( endTime.doubleValue() - startTime.doubleValue() ) / 1E9 ;
        System.out.println(
            "Wrapped runtime: " + duration + " seconds."
        ) ;
        System.out.println(
            "Experiment completed."
        ) ;
    }
}

结果如下:

Experiment started.
Unwrapped runtime: 4.851473465 seconds.
Wrapped runtime: 5.078349508 seconds.
Experiment completed.

为什么会发生这种情况?如何使JVM内联方法或以其他方式对其进行优化以使包装器被忽略?

谢谢。

乐观,为迭代选择final Long maxValue,但随后将100用作MaxValue。

如果您用ints更换long,则可能会得到10倍的速度。

第二和第三,急剧的改进是循环直到数学。

 for (int j = 2; j <= Math.sqrt(i) && isPrime; ++j)

我知道,您并不是一个有效的Prime查找算法的背后,但这全都是针对包装器的微型计算标记,但是要学习的基本课程是,此类假设在大多数情况下不是一堆应用程序的瓶颈。

在此旁边,您应该在相反的方向上尝试测试,首先包裹,然后取消包装。为了简单地进行此类更改,您应该考虑循环和时间安排。

import java.util.* ;
public class WrappingTest1
{
    PrimeFinder[] pfs = new PrimeFinder[2];
    int primeRange = 1000;
    private WrappingTest1 ()
    {
        // pfs[1] = new UnwrappedFinder ();
        // pfs[0] = new WrappedFinder (pfs[1]);
        pfs[0] = new UnwrappedFinder ();
        pfs[1] = new WrappedFinder (pfs[0]);
    }
    void test ()
    {
        for (PrimeFinder pf: pfs)
            runblock (pf);
    }
    void loopy (int iterations, PrimeFinder pf, ArrayList <Integer> primes)
    {
        for (int i = 0; i < iterations; ++i)
            pf.findPrimes (primeRange, primes);
    }
    void runblock (PrimeFinder pf)
    {
        int warmupIterations = 20000;
        int benchmarkIterations = 100000;
        ArrayList <Integer> primes = new ArrayList<Integer> (50000) ;
        // warmup.
        loopy (warmupIterations, pf, primes);
        // enchmark.
        Long startTime = System.nanoTime();
        loopy (benchmarkIterations, pf, primes);
        Long endTime = System.nanoTime() ;
        Double duration = (endTime.doubleValue () - startTime.doubleValue ()) / 1E9 ;
        System.out.printf ("%s runtime: %4.2f seconds.n", pf.name(), duration);
        // had to make sure, that we're really producing valid primes:
        // and that they survive the code changes.
        for (int p: primes) {
            System.out.printf ("%d ", p);
        }
        System.out.println ("bye");
    }
    abstract class PrimeFinder {
        abstract void findPrimes (final int maxValue, final List <Integer> foundPrimes);
        abstract String name ();
    }
    class UnwrappedFinder extends PrimeFinder {
        String name () {return "Unwrapped";}
        void findPrimes (final int maxValue, final List <Integer> foundPrimes)
        {
            if (maxValue > 2)
            {
                foundPrimes.clear () ;
                for (int i = 2; i <= maxValue; ++i)
                {
                    Boolean isPrime = true;
                    for (int j = 2; j <= Math.sqrt(i) && isPrime; ++j)
                        if ((i % j) == 0)
                            isPrime = false;
                    if (isPrime)
                        foundPrimes.add (i);
                }
            }
        }
    }
    class WrappedFinder extends PrimeFinder {
        String name () {return "  Wrapped";}
        private PrimeFinder pf;
        public WrappedFinder (PrimeFinder ppf)
        {
            pf = ppf;
        }
        void findPrimes (final int input, final List <Integer> output) {
            pf.findPrimes (input, output);
        }
    }
    public static void main (final String[] args)
    {
        System.out.println ("Experiment started.");
        WrappingTest1 wt1 = new WrappingTest1 ();
        wt1.test ();
        System.out.println ("Experiment completed.") ;
    }
}

使用Primerange = 100运行代码,但是1M迭代,我得到:

Unwrapped runtime: 2,34 seconds.
Unwrapped runtime: 2,53 seconds.
Unwrapped runtime: 2,50 seconds.
Unwrapped runtime: 2,50 seconds.
Unwrapped runtime: 2,49 seconds.
Unwrapped runtime: 2,52 seconds.
Unwrapped runtime: 2,59 seconds.
Unwrapped runtime: 2,60 seconds.
Unwrapped runtime: 2,58 seconds.
Unwrapped runtime: 2,52 seconds.
  Wrapped runtime: 2,36 seconds.
  Wrapped runtime: 2,36 seconds.
  Wrapped runtime: 2,36 seconds.
  Wrapped runtime: 2,36 seconds.
  Wrapped runtime: 2,37 seconds.
  Wrapped runtime: 2,37 seconds.
  Wrapped runtime: 2,36 seconds.
  Wrapped runtime: 2,41 seconds.
  Wrapped runtime: 2,37 seconds.
  Wrapped runtime: 2,37 seconds.

因此,令人惊讶的是,包装版本更快。嗯。更改PrimeFinder [] PFS中的顺序,它们更近,包裹着:2.46,未包装2.52。

看来,放缓的原因不是包装/未包装方法调用的优化(或缺乏(,而是缺乏对主方法本身的优化。用-XX进行分析:-printCompilation表明,根据默认设置(-XX:CompileTherShord = 10000(,包装/未包装的方法调用在10K热身迭代后进行了优化。但是,只有在大约70k的热身迭代之后,JVM分析报告了主要方法。因此,如果热身迭代小于70k,则未包装的运行时明显低于包装的运行时;但是,如果热身迭代为70k及以上,则两个跑步时间相似。当然,这仅适用于指定的基准测试 - 单个程序可能会产生不同的结果。

最新更新