Solved

Performance issue while iterating with streams

Posted on 2016-09-02
5
48 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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 IE Certificate Issue 10 51
Need Help! Getting a syntax error and don't understand why 3 31
learn programming 8 42
passing enum to a method 4 23
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

830 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