Git Product home page Git Product logo

java11-stream-parallel-linkedlist-vs-arraylist's Introduction

Build Status

java11-stream-parallel-linkedlist-vs-arraylist

Reference: http://alexandrastech.blogspot.com/2016/04/java-8-parallel-streams-performance.html
Reference: https://www.amazon.com/Modern-Java-Action-functional-programming/dp/1617293563

preface

Source Decomposability
ArrayList Good
LinkedList Poor
IntStream.range Excellent
Stream.iterate Poor
HashSet Good
TreeSet Good

ArrayList can be split much more efficiently than a LinkedList, because the first can be evenly divided without traversing it (implements RandomAccess), as it’s necessary to do with the latter.

The stream is internally splitted using Spliterator - please refer my other github projects:

project description

We provide basic comparison of parallel streams over ArrayList and LinkedList.

  • compare
    • ArrayList
      var integers = new ArrayList<Integer>();
      
      setAll(integers);
      
      System.out.println(timeOfSumming(integers));
      
      time: 60
    • LinkedList
      var integers = new LinkedList<Integer>();
      
      setAll(integers);
      
      System.out.println(timeOfSumming(integers));
      
      time: 400
  • summing in parallel
    private int parallelSum(List<Integer> values) {
        return values.parallelStream().mapToInt(i -> i).sum();
    }
    
  • count time of summing
    private long timeOfSumming(List<Integer> integers) {
        long startTime = System.currentTimeMillis();
        parallelSum(integers);
        return System.currentTimeMillis() - startTime;
    }
    
  • fill lists with random numbers
    private void setAll(List<Integer> integers) {
        Random random = new Random();
        IntStream.range(1, 10_000_000)
                .forEach(i -> integers.add(random.nextInt(500)));
    }
    

java11-stream-parallel-linkedlist-vs-arraylist's People

Contributors

mtumilowicz avatar

Watchers

 avatar  avatar

Forkers

sfbayman

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.