Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Performance issue while iterating with streams

Posted on 2016-09-02
5
Medium Priority
?
59 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

972 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