java-如何加快计算速度?

java-如何加快计算速度?,java,multithreading,concurrency,Java,Multithreading,Concurrency,我的函数是total+=Math.sqrt(num)-Math.cbrt(num)我想将其应用于每个数字,直到确定最大值(从0开始)。所以我在下面写了一些代码,它使用除法技术来加快计算速度。我怎样才能加快计算?下面的代码(8个线程)需要20秒才能完成,而非线程需要150秒才能完成。我相信通过forkjoinpool我可以让它更快,或者并行流?如何与他们一起实施 public class Main { private static int targetNum = Integer.MAX_

我的函数是
total+=Math.sqrt(num)-Math.cbrt(num)我想将其应用于每个数字,直到确定最大值(从0开始)。所以我在下面写了一些代码,它使用除法技术来加快计算速度。我怎样才能加快计算?下面的代码(8个线程)需要20秒才能完成,而非线程需要150秒才能完成。我相信通过
forkjoinpool
我可以让它更快,或者
并行流
?如何与他们一起实施

public class Main {

    private static int targetNum = Integer.MAX_VALUE;
    private static int threadCount = 8;
    private static double total = 0;
    public static void main(String[] args) {
    // write your code here
        DecimalFormat df2 = new DecimalFormat(".##");
        long time = System.currentTimeMillis();


        ExecutorService executor = Executors.newFixedThreadPool(threadCount);

        try {
            ArrayList<Future<Double>> futureList = new ArrayList<>();
            for(int a = 0; a < threadCount; a++){
                calculatorService ss = new calculatorService(a*(targetNum/threadCount) ,(a+1) *(targetNum/threadCount));
                futureList.add(executor.submit(ss));
            }
            for(int a = 0; a < threadCount; a++){
                total+= futureList.get(a).get();
            }

            System.out.println("Result= "+ df2.format(total) + "\nTime passed= " + ((System.currentTimeMillis() - time)/1000f));
            executor.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
class calculatorService implements Callable<Double>{

    private int start,end;

    public SqrtSummer(int start, int end) {
        this.start = start;
        this.end = end;
    }

    @Override
    public Double call(){
        double total = 0;
        for (int a = start; a < end; a++) {
            total += Math.sqrt(a) - Math.cbrt(a);
        }
        return total;
    }
}
公共类主{
私有静态int targetNum=Integer.MAX_值;
私有静态int threadCount=8;
私有静态双倍合计=0;
公共静态void main(字符串[]args){
//在这里编写代码
DecimalFormat df2=新的DecimalFormat(“.###”);
长时间=System.currentTimeMillis();
ExecutorService executor=Executors.newFixedThreadPool(线程计数);
试一试{
ArrayList futureList=新的ArrayList();
对于(int a=0;a
编辑1


futureList.get(a.get()我必须这样做,因为我不知道线程(核心)计数。因此,我无法编写futureList.get(0.get()+futureList.get(1.get()。。。。。我知道直到futureList.get(0).get()循环将等待,但它们仍将完成自己的工作。我的线程数不是固定的,可以随时更改。

当您的应用程序是I/O密集型应用程序时,多线程可以从中受益。但是,此应用程序对计算敏感,可能会指定
threadCount
处理器的数量是最佳选择:

private static int threadCount = Runtime.getRuntime().availableProcessors();

当应用程序是I/O密集型应用程序时,多线程可能会受益。但是,此应用程序对计算敏感,可能会指定
threadCount
处理器的数量是最佳选择:

private static int threadCount = Runtime.getRuntime().availableProcessors();
导入java.text.DecimalFormat;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.concurrent.Callable;
导入java.util.concurrent.ExecutionException;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.Future;
公共班机{
私有静态int targetNum=Integer.MAX_值;
private static int threadCount=Runtime.getRuntime().availableProcessors();
私有静态双倍合计=0;
公共静态void main(字符串[]args){
//在这里编写代码
DecimalFormat df2=新的DecimalFormat(“.###”);
长时间=System.currentTimeMillis();
List futureList=新建ArrayList();
int lastSize=futureList.size();
ExecutorService executor=Executors.newFixedThreadPool(线程计数);
Runnable sumRunnable=()->
{int sumculatedtill=0;
而(!executor.isTerminated())
{
if(lastSize!=futureList.size())
{
对于(int i=sumCalculatedTill;i
import java.text.DecimalFormat;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.concurrent.Callable;
导入java.util.concurrent.ExecutionException;
导入java.util.concurrent.ExecutorService;
导入java.util.concurrent.Executors;
导入java.util.concurrent.Future;
公共班机{
私有静态int targetNum=Integer.MAX_值;
private static int threadCount=Runtime.getRuntime().availableProcessors();
私有静态双倍合计=0;
公共静态void main(字符串[]args){
//在这里编写代码
DecimalFormat df2=新的DecimalFormat(“.###”);
长时间=System.currentTimeMillis();
List futureList=新建ArrayList();
int lastSize=futureList.size();
ExecutorService executor=Executors.newFixedThreadPool(线程计数);
Runnable sumRunnable=()->
{int sumculatedtill=0;
而(!executor.isTerminated())
{
if(lastSize!=futureList.size())
{

对于(int i=sumCalculatedTill;i如何将计算减少到1微秒?简单

你们的总数是:n个平方根的和-n个立方根的和

数学小贴士:

double sumOfSquareRoots = 2D * Math.pow((n + 0.5D), 1.5D) / 3D - 0.22474487139;

有关立方根,请参见:)

如何将计算减少到1微秒?简单

你们的总数是:n个平方根的和-n个立方根的和

数学小贴士:

double sumOfSquareRoots = 2D * Math.pow((n + 0.5D), 1.5D) / 3D - 0.22474487139;

有关立方根,请参见:)

如果您有8个核,并且顺序时间是150秒,那么使用相同的算法并在8个核上进行并行处理,您所能期望的最佳时间是150/8=18.75秒。因此,20秒非常接近,如果不添加核或更改算法,您不会得到比这更好的结果。@JBNizet所以您说的是设计明智的我的实现n是可以的,但我需要找到一种更快的方法来处理Math.sqrt等?如果你有8个核,并且顺序时间是150秒,那么使用相同的算法并在8个核上并行它,你能期望的最佳时间是150/8=18.75秒。因此20秒非常接近,你不会比使用