Solved

Performance issue while iterating with streams

Posted on 2016-09-02
5
53 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2

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 2

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

627 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