Skip to main content

Posts

Showing posts from May, 2017

Java 8 streams performance on mathematical calculations

Java 8 Streams API supports many parallel operations to process the data, it abstracts low level multithreading logic. To test performance did following simple test to calculate factorial of first X number starting with N. Following program calculates factorial of first 1000 numbers. package com.java.examples ; import java.math.BigInteger ; import java.time.Duration ; import java.time.Instant ; import java.util.ArrayList ; import java.util.List ; public class MathCalculation { public static void main ( String [] args ) { List < Runnable > runnables = generateRunnables ( 1 , 1000 ) ; Instant start = Instant . now ( ) ; // Comment one of the lines below to test parallel or sequential streams runnables . parallelStream ( ) . forEach ( r - > r . run ( ) ) ; // runnables.stream().forEach(r -> r.run()); Instant end = Instant . now ( ) ; System . out . println ( " Calculated in " + Duration . between ( start , end )