zoukankan      html  css  js  c++  java
  • 代码性能提升10倍(ForkJoin)

    package com.lyl.test;
    
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.Random;
    import java.util.concurrent.ForkJoinPool;
    import java.util.concurrent.RecursiveTask;
    
    /**
     * @Auther: lyl
     * @Date: 2020/03/31 11:06
     * @Description:
     */
    public class Test6 {
        int[] nums = new int[10_000_0000];
        int CI = 10;//计算多少次
        Random r = new Random();
    
        public Test6() {
            for (int i = 0; i < nums.length; i++) {
                nums[i] = r.nextInt(10);
            }
        }
    
        public static void main(String[] args) throws IOException {
    //        Arrays.stream(new int[3]).sum();//初始化sum内部排除初始化时间
    
            Test6 t = new Test6();
            t.aa();
            t.bb();
            t.cc();
        }
    
        public void aa() {
    
    
            long start = System.currentTimeMillis();
            long n = 0;
            for(int i = 0; i< CI; i++) {
                n = Arrays.stream(nums).sum();
            }
            long end = System.currentTimeMillis();
            System.out.println("aa|"+ (end - start )+ "ms|" + n);
        }
    
        public void bb() {
            long start = System.currentTimeMillis();
            long n = 0;
            for(int j = 0; j< CI; j++) {
                n = 0;
                for (int i = 0; i < nums.length; i++) {
                    n += nums[i];
                }
            }
    
            long end = System.currentTimeMillis();
            System.out.println("bb|"+ (end - start )+ "ms|" + n);
        }
    
    
        public void cc() throws IOException {
            Long invoke = 0L;
            ForkJoinPool pool = new ForkJoinPool();
            AddTask task = new AddTask(0,nums.length);
            long start = System.currentTimeMillis();
            for(int j = 0; j< CI; j++) {
    //            pool.execute(task);
                invoke = pool.invoke(task);
            }
    
    //        long s = task.join();
            long end = System.currentTimeMillis();
    
            System.out.println("cc|"+ (end - start )+ "ms|" + invoke);
            System.in.read();
        }
    
        class AddTask extends RecursiveTask<Long> {
    
            int start;
            int end;
    
            public AddTask(int s,int e){
                this.start = s;
                this.end = e;
            }
    
            @Override
            protected Long compute() {
    
                if(end - start <= 50000){
                    long sum = 0L;
                    for(int i = start;i< end ;i++){
                        sum += nums[i];
    
                    }
                    return sum;
                }
                int m = start +(end-start)/2;
    
                AddTask sub1 = new AddTask(start,m);
                AddTask sub2 = new AddTask(m,end);
                sub1.fork();
                sub2.fork();
    
                return sub1.join()+sub2.join();
    
            }
        }
    }

  • 相关阅读:
    【火炉炼AI】机器学习003-简单线性回归器的创建,测试,模型保存和加载
    【火炉炼AI】机器学习002-标记编码方法
    【火炉炼AI】机器学习001-数据预处理技术(均值移除,范围缩放,归一化,二值化,独热编码)
    HOJ 13830 DNA Sequencing
    HDU 6078 Wavel Sequence
    hoj 13814 Cake cut
    HDU6071 Lazy Running
    HDU 6070 Dirt Ratio
    HDU 6070 Dirt Ratio
    HDU 6059 Kanade's trio
  • 原文地址:https://www.cnblogs.com/liaoyanglong/p/12603801.html
Copyright © 2011-2022 走看看