1

I have requirement that to read data from properties file which is command separated which will be obviously in string form. So after that I am splitting it into string array.

I have requirement to convert that string array to integer list.

I have tried 2 ways:

  1. Traditional for each loop with casting,
  2. use java 8 stream.

Then I think I have to calculate the time which one is good as per performance. So I have some rough data.

Code:

public class PropertyLoadCSV { private static Properties properties; public static void main(String[] args) { try { properties = new Properties(); properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("resources/csvfile.properties")); String ids = properties.getProperty("ids"); String[] splitedIDs = ids.split(","); long startTime = System.nanoTime(); convertUsingJava7(splitedIDs); long endTime = System.nanoTime(); long duration = (endTime - startTime); System.out.println(duration); long startTime1 = System.nanoTime(); convertUsingJava8(splitedIDs); long endTime1 = System.nanoTime(); long duration1 = (endTime1 - startTime1); System.out.println(duration1); } catch (Exception e) { e.printStackTrace(); } } static List<Integer> convertUsingJava7(String[] splited){ List<Integer> list = new ArrayList<>(); for(String s: splited){ list.add(Integer.valueOf(s)); } return list; } static List<Integer> convertUsingJava8(String[] splited){ return Stream.of(splited).map(Integer::parseInt).collect(Collectors.toList()); } } 

got some unexpected result in console:

131601

254094088

so really java 8 stream convert and cast slowly then tradition way ?

Which one I have to use as performance concern ?

Here's working example for it.

1 Answer 1

4

you are measuring the cold start here, that is a single method invocation without giving JIT any chance do something good for you.

The first invocation of a lambda expression is always slow because of invokedynamic bootstrap( it has to create the instance were the actual method will get called, but that is a penalty you have to pay only once).

Use a tool like jmh to correctly measure the results and also either way, your stardard code will be faster then streams, which is normal, you have to pay a price for the stream infrastracture that gets created.

Sign up to request clarification or add additional context in comments.

3 Comments

hello seems pretty much interesting.. second iteration call makes it quite faster. Even standard code works too faster second time. So as per you which one is better if this functionally will be going to use 20-50 times an hour(minimum call) ?
50 times an hour? :) you have measured nano seconds there... choose whatever you like, its not a performance bottelneck.
@user3145373: the loading of the properties will outweigh everything else in an order of magnitude. As soon, as you are executing this a second time, there will be no performance difference between the loop and the stream use anyway. But as a side note, if you have an existing array, you should use Arrays.stream(array) rather than the Stream.of(varargs) method. And there’s no reason to change the semantic, you can still use Integer::valueOf, like in your loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.