• 代码性能提升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();
    
            }
        }
    }

  • 相关阅读:
    Python 2 中的编码
    奇异值分解及其应用
    c#基础系列3---深入理解ref 和out
    c#基础系列2---深入理解 String
    c#基础系列1---深入理解值类型和引用类型
    广州.NET微软技术俱乐部微信群有用信息集锦(10)
    程序员英语二三事(3)
    BDD实战篇
    BDD实战篇
    广州.NET微软技术俱乐部
  • 原文地址:https://www.cnblogs.com/liaoyanglong/p/12603801.html
Copyright © 2020-2023  润新知