Solved

Performance issue while iterating with streams

Posted on 2016-09-02
5
39 Views
Last Modified: 2016-09-14
Hi Experts,

I have written the below program using java7 and java8

Collections are giving me result quickly when i compared with streams
Is any thing wrong in my code.
can some one suggest me what exactly the issue with streams iteration.

Collections is taking only 16 milliseconds
FROM STREAMS
localCaloricDishesName  [EggCurry, BeatRootCurry, LadyFinger]
109 millis

package com.java8;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class StreamAndFilter  {

	public static void main1(String[] args) {
		Long startTime = System.currentTimeMillis();	
		List<Dish> menu = new ArrayList<>();
		menu.add(new Dish("CarrotCurry",405));
		menu.add(new Dish("BeatRootCurry",215));
		menu.add(new Dish("PotatosCurry",450));
		menu.add(new Dish("TamatoCurry",500));
		menu.add(new Dish("LadyFinger",300));
		menu.add(new Dish("EggCurry",200));
		
		List<Dish> localCaloricDishesName = new ArrayList<>();
		
		for (Dish dish : menu) {
			if(dish.getCalories()<400){
				localCaloricDishesName.add(dish);
			}
		}
		
		Collections.sort(localCaloricDishesName,new Comparator<Dish>() {

			@Override
			public int compare(Dish o1, Dish o2) {
				// TODO Auto-generated method stub
				return Integer.compare(o1.getCalories(), o2.getCalories());
			}
			
		});
		
		List<String> curryNames = new ArrayList<String>();
		for (Dish dish : localCaloricDishesName) {
			curryNames.add(dish.getDishName());
		}
		
		System.out.println("curryNames"+curryNames);;
		System.out.println(System.currentTimeMillis()-startTime);
	}
	
	public static void main(String[] args) {
		System.out.println("FROM STREAMS");
		Long startTime = System.currentTimeMillis();
		List<Dish> menu = new ArrayList<>();
		menu.add(new Dish("CarrotCurry",405));
		menu.add(new Dish("BeatRootCurry",215));
		menu.add(new Dish("PotatosCurry",450));
		menu.add(new Dish("TamatoCurry",500));
		menu.add(new Dish("LadyFinger",300));
		menu.add(new Dish("EggCurry",200));
		
		List<String> localCaloricDishesName = 
				menu.stream()
				.filter(d->d.getCalories()<400)
				.sorted(Comparator.comparing(Dish :: getCalories))
				.map(Dish::getDishName)
				.collect(Collectors.toList());
		
		System.out.println("localCaloricDishesName  "+localCaloricDishesName);
		System.out.println(System.currentTimeMillis()-startTime);		
	}
}

Open in new window

0
Comment
Question by:srikotesh
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 41781956
I don't see anything obviously wrong with your code; streams just have higher processing overhead than enhanced for loops. The difference is probably smaller when you're dealing with larger collections. Streams would definitely use less memory, when processing large collections, because you're not making so many intermediate collections along the way.
1
 
LVL 1

Author Comment

by:srikotesh
ID: 41789421
Hi CPColin,

IF possible can you show me a sample example which improves performance.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41789608
Nope. To reiterate what I said in my last comment: Your code is fine; streams just have more overhead.
0
 
LVL 1

Author Comment

by:srikotesh
ID: 41790138
Memory wise streams are better than collection
Performance wise stream slower than collection
AM I correct?
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41790158
It depends entirely on what you're doing. My standard advice is that you should focus on writing code that clearly shows your intent and worry about performance if and when it becomes a problem.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now