Java中的线程阶乘性能

Java中的线程阶乘性能,java,multithreading,factorial,Java,Multithreading,Factorial,我在stackoverflow上搜索Java中阶乘的多线程变体,但令我惊讶的是,竟然没有。所以我想自己做,因为我想看到性能的提高。(我在这里为搜索此代码的任何人共享代码。) 现在我想比较一下它与单线程迭代方法和递归方法的性能。但我看不到它的单线程对应物有任何性能改进,其值高达1200!(Long数据类型不能保存值21!因为它大于Long.MAX_值。)那么如何使用更大的数字进行比较呢?我使用的是biginger和1232!是两种方法得到的最大值 import java.math.BigInteg

我在stackoverflow上搜索Java中阶乘的多线程变体,但令我惊讶的是,竟然没有。所以我想自己做,因为我想看到性能的提高。(我在这里为搜索此代码的任何人共享代码。) 现在我想比较一下它与单线程迭代方法和递归方法的性能。但我看不到它的单线程对应物有任何性能改进,其值高达1200!(Long数据类型不能保存值21!因为它大于Long.MAX_值。)那么如何使用更大的数字进行比较呢?我使用的是biginger和1232!是两种方法得到的最大值

import java.math.BigInteger;
import java.util.Arrays;

public class MultiThreadedFactorial
{
    public static void main(String[] args) throws InterruptedException
    {
        long startTime=System.nanoTime();

        int x=1232;

        if(x<2) return;

        int[] array=new int[x-1];//It is x-1 because of 1 is not included in 2...10.
        for(int i=0;i<array.length;i++)
        {
            array[i]=i+2;
        }

        int[] partA=Arrays.copyOfRange(array,0,array.length/3);//Dividing array into 3 equal parts
        PartialProduct A=new PartialProduct(partA);

        int[] partB=Arrays.copyOfRange(array,array.length/3,2*array.length/3);
        PartialProduct B=new PartialProduct(partB); 

        int[] partC=Arrays.copyOfRange(array,2*array.length/3,array.length);
        PartialProduct C=new PartialProduct(partC);

        A.start();
        B.start();
        C.start();

        A.join();
        B.join();
        C.join();

        BigInteger bi=new BigInteger(A.product+"");
        bi=bi.multiply(new BigInteger(B.product+""));
        bi=bi.multiply(new BigInteger(C.product+""));

        System.out.println(bi.toString());

        long endTime=System.nanoTime();
        float totalTime=(endTime-startTime)/1000000000f;
        System.out.println("Took "+totalTime+" seconds!");
    }

    static long multiply(long... numbers)
    {
        long factorial=1;
        for(long n:numbers)
            factorial*=n;
        return factorial;
    }

}

class PartialProduct extends Thread
{
    int[] numbers;
    BigInteger product=new BigInteger("1");

    public PartialProduct(int[] numbers)
    {
        this.numbers=numbers;
    }

    public void run()
    {
        for(int n:numbers)
            product=product.multiply(BigInteger.valueOf(n));
    }
}
import java.math.biginger;
导入java.util.array;
公共类多线程阶乘
{
公共静态void main(字符串[]args)引发InterruptedException
{
long startTime=System.nanoTime();
int x=1232;

如果(x使用Java 8中可用的Java流API编写factorial的并行版本要容易得多

单线程实现可能如下所示:

  static BigInteger sequentialFactorial(int n)
  {
    if (n < 2)
      return BigInteger.ONE;
    return IntStream.rangeClosed(2, n)
        .sequential()
        .mapToObj(BigInteger::valueOf)
        .reduce(BigInteger.ONE, BigInteger::multiply, BigInteger::multiply);
  }
static biginger sequentialFactorial(int n)
{
if(n<2)
返回BigInteger.1;
return IntStream.rangeClosed(2,n)

.mapToObj(biginger::valueOf)
.reduce(biginger.ONE,biginger::multiply,biginger::multiply);
}
并行/多线程版本是一个微不足道的变化:

  static BigInteger parallelFactorial(int n)
  {
    if (n < 2)
      return BigInteger.ONE;
    return IntStream.rangeClosed(2, n)
        .parallel()
        .mapToObj(BigInteger::valueOf)
        .reduce(BigInteger.ONE, BigInteger::multiply, BigInteger::multiply);
  }
静态BigInteger并行阶乘(int n)
{
if(n<2)
返回BigInteger.1;
return IntStream.rangeClosed(2,n)
.parallel()
.mapToObj(biginger::valueOf)
.reduce(biginger.ONE,biginger::multiply,biginger::multiply);
}

如果您以合理的方式计算这些时间(例如,1000次迭代,每次计算1000!),您应该可以在多核机器上看到显著的加速效果。

也许可以看看竞争性单线程算法。我想知道为什么问题会变得越来越糟,我猜是因为您并不是真的在问问题。如果您找到了问题的解决方案,并想分享这一点,正确的方法是发布一个问题,然后然后回答你自己的问题。很好。我会把它转换成自我回答的风格