Loading...

Iteration Over Java Collections with High Performance

View: 147    Dowload: 0   Comment: 0   Post by: admin   Category: Javascript   Fields: Other

Travelling over Java collections just a piece of cake, but when size of collections increase you have to choose wisely

Introduction

Java developers usually deal with Collections such as ArrayListHashSet, Java 8 come with lambda and streaming API helps us to easily work with Collections. In most cases, we work with few thousands of items and performance isn't a concern. But in strict code, when we have to travel over millions items several times, performance will become a pain.

forEach vs C style vs Stream API

Iteration is a basic feature so that all programming languages have simple syntax to allow programmers to run through collectionsStream API can iterate over Collections in a very straightforward manner.

public void streamSingleThread(BenchMarkState state){
    state.testData.stream().forEach(item -> {
        // Do something
    });
}

forEach loop is also simple:

public void forEach(BenchMarkState state){
    for(Integer temp : state.testData){
        // Do something
    }
}

C style is more verbose, but still very compact:

public void forCStyle(BenchMarkState state){
    int size = state.testData.size();
    for(int j = 0; j < size; j ++){
        state.testData.get(j);
        // Do something
    }
}

Then the performance:

Benchmark                               Mode  Cnt        Score       Error  Units
TestLoopPerformance.forCStyle           avgt  200        3.182 ±     0.009  ns/op
TestLoopPerformance.forEach             avgt  200  7693143.747 ± 57712.787  ns/op
TestLoopPerformance.streamMultiThread   avgt  200  6974017.140 ± 66200.317  ns/op
TestLoopPerformance.streamSingleThread  avgt  200  7790435.456 ± 75683.894  ns/op

With C style, JVM just simply increases an integer, then reads value directly from memory, so that it is very fast. But forEach is very different, according to this answer on StackOverFlow and document from Oracle, JVM has to convert forEach to Iterator and call hasNext() with every item, that's why forEach is much slower than C style.

 

Which is High Performance Way to Travelling Over Set?

With data:

@State(Scope.Benchmark)
public static class BenchMarkState {
    @Setup(Level.Trial)
    public void doSetup() {
        for(int i = 0; i < 500000; i++){
            testData.add(Integer.valueOf(i));
        }
    }
    @TearDown(Level.Trial)
    public void doTearDown() {
        testData = new HashSet<>(500000);
    }
    public Set<Integer> testData = new HashSet<>();
}

Java Set also supports Stream API and forEach loop, according to the previous test, should we wrap Set to ArrayList, then travel over ArrayList?

public void forCStyle(BenchMarkState state){
    int size = state.testData.size();
    Integer[] temp = (Integer[]) state.testData.toArray(new Integer[size]);
    List<Integer> tempList = new ArrayList<>(state.testData.size());
    for(int j = 0; j < size; j ++){
        tempList.add(temp[j]);
    }
}

How about combine iterator with C style for loop?

public void forCStyleIterator(BenchMarkState state){
    int size = state.testData.size();
    List<Integer> tempList = new ArrayList<>(state.testData.size());
    Iterator<Integer> iteration = state.testData.iterator();
    for(int j = 0; j < size; j ++){
        tempList.add(iteration.next());
    }
}

Or just simple travel?

public void forEach(BenchMarkState state){
     List<Integer> tempList = new ArrayList<>(state.testData.size());
     for(Integer temp : state.testData){
         tempList.add(temp);
     }
 }

Nice idea, but it doesn't work because initialize new ArrayList also consumes resources.

Benchmark                             Mode  Cnt        Score        Error  Units
TestLoopPerformance.forCStyleIterator avgt  200  4344457.729 ±  67534.559  ns/op
TestLoopPerformance.forEach           avgt  200  4553071.127 ±  63691.874  ns/op
TestLoopPerformance.forCStyle         avgt  200  6585935.929 ± 151084.531  ns/op

HashMap (HashSet uses HashMap<E,Object>) isn't designed for iterating all items, the fastest way to iteration over HashMap is a combination of Iterator and C style for loop, because JVM doesn't have to call hasNext().

Conclusion

Foreach and Stream API are convenient to work with Collections, you can write code faster. But when your system is stable and performance is a major concern, you should think of rewriting your loop.

Iteration Over Java Collections with High Performance

Java developers usually deal with Collections such as ArrayList, HashSet, Java 8 come with lambda and streaming API helps us to easily work with Collections. In most cases, we work with few thousands of items and performance isn't a concern. But in strict code, when we have to travel over millions items several times, performance will become a pain.

Posted on 06-03-2018 

Comment:

To comment you must be logged in members.

Files with category

  • Mini Youtube Using ReactJS

    Mini Youtube Using ReactJS

    View: 25    Download: 2   Comment: 0

    Category: Javascript     Fields: none

    This is one the best starter for ReactJS. MiniYoutube as the name suggests is a youtube like website developed using reactJS and youtube API. This project actually let's you search , play and list youtube videos. Do check it out and start learning...

  • Angular 6 Starter with Laravel 5.6 API Service

    Angular 6 Starter with Laravel 5.6 API Service

    View: 62    Download: 0   Comment: 0

    Category: Javascript     Fields: none

    Angular 6 and Laravel 5.6 This project is a starter for creating interface with Angular using bootstrap && css && sass and using Laravel 5.6 for api requests. Demo Installation This project is divided in two parts (projects) and before use them you...

  • Simple Richtext Editor Based on pellJS

    Simple Richtext Editor Based on pellJS

    View: 26    Download: 0   Comment: 0

    Category: Javascript     Fields: none

    A simple visual editor for websites using the pell javascipt. It also has the option to switch between visual editor mode and source code mode. I will upload an update for new functionality soon. Source Code Editor Visual Editor

  • Data Visualization for BI: How to Design Layouts for .NET Financial Reports

    Data Visualization for BI: How to Design Layouts for .NET Financial Reports

    View: 37    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    With the Active Reports Server, you can have a multi-tenant environment where users from various departments, companies, or other specifications can log in, view their reports (and only their reports), export the data, or set up a distribution...

  • AngularJS and REST API

    AngularJS and REST API

    View: 204    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    This is a tutorial for those interested in a quick introduction to AngularJS and REST API. We will build the familiar Periodic Table of the Elements found in every chemistry textbook, and allow the user to select a Chemical Element by clicking on...

  • Collective Intelligence, Recommending Items Based on Similar Users' Taste

    Collective Intelligence, Recommending Items Based on Similar Users' Taste

    View: 168    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Using Collaborative Filtering to find people who share tastes, and for making automatic recommendations based on things that other people like.

  • Think Like a Bird for Better Parallel Programming

    Think Like a Bird for Better Parallel Programming

    View: 157    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Coding an application to run in parallel is hard, right? I mean, it must be hard or we’d see parallel programs everywhere. All we'd see are slick parallel apps that use every available core effortlessly. Instead multi-threaded apps are the exception...

  • Getting Started with the Bing Search APIs

    Getting Started with the Bing Search APIs

    View: 169    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Bing Search API is a set of REST interfaces that find web pages, news, images, videos, entities, related searches, spelling corrections, and more in response to queries from any programming language that can generate a web request. Applications that...

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd