Comparison functional code vs imperative code

Java imperative vs functional in 2025 — revisiting a 2015 microbenchmark

Quick numbers (avg; smaller is faster) I (imperative nested): 3.28 µs I2 (imperative freq-map): 1.93 µs F (streams grouping): 127.37 µs FP (parallel streams grouping): 599.28 µs Winner: I2 — imperative freq-map Note: These are sample numbers from the run below on my machine; yours will differ. I/F labels mirror the 2015 post for a simple visual compare. == 2015-style harness (I:/F: lines) == ozkan@ozkan-debian:~/projects/ozkanpakdil.github.io/scripts/compare-2015-25$ ./run.sh javac 25 I:5372 F:22032373 I:5816 F:186352 F:144816 F:134903 F:107685 I:4919 I:4903 I:4698 I:4147 F:104857 == 2025 benchmark summary (fastest → slowest) == ...

September 29, 2025 · 3 min · Özkan Pakdil

Java imperative and functional approach performance test 2

another test for imperative code package testarea; import java.util.ArrayList; import java.util.List; import java.util.OptionalDouble; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Test { static int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10 }; private static List<Double> NUMBERS_FOR_AVERAGE = new ArrayList<Double>(); static long startTime, stopTime; public static void main(String[] args) { NUMBERS_FOR_AVERAGE.add(new Double(10)); NUMBERS_FOR_AVERAGE.add(new Double(10)); NUMBERS_FOR_AVERAGE.add(new Double(10)); NUMBERS_FOR_AVERAGE.add(new Double(10)); NUMBERS_FOR_AVERAGE.add(new Double(10)); NUMBERS_FOR_AVERAGE.add(new Double(10)); NUMBERS_FOR_AVERAGE.add(new Double(10)); NUMBERS_FOR_AVERAGE.add(new Double(10)); NUMBERS_FOR_AVERAGE.add(new Double(10)); NUMBERS_FOR_AVERAGE.add(new Double(10)); runImperative(); runFunctional(); runImperative(); runFunctional();runFunctional();runFunctional();runFunctional(); runImperative();runImperative();runImperative();runImperative(); runFunctional(); } private static void runFunctional() { startTime = System.nanoTime(); functionalApproach(); stopTime = System.nanoTime(); System.out.println("F:"+(stopTime - startTime)); } private static void runImperative() { startTime = System.nanoTime(); imperativeApproach(); stopTime = System.nanoTime(); System.out.println("I:"+(stopTime - startTime)); } private static void imperativeApproach() { Double sum = 0d; for (Double vals : NUMBERS_FOR_AVERAGE) { sum += vals; } sum = sum / NUMBERS_FOR_AVERAGE.size(); } private static void functionalApproach() { OptionalDouble average = NUMBERS_FOR_AVERAGE .stream() .mapToDouble(a -> a) .average(); } } here is my output ...

September 20, 2015 · 1 min · Özkan Pakdil

Java imperative and functional approach performance test

I love performance tests. generally I test everything myself if there is no source in the internet I must do it :) today I was reading imperative coding vs functional coding here. it stuck my mind this sentence. they’re probably equally fast and reasonable then I have to try which one is faster. here is the code package testarea; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Test { static int[] array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 8, 8, 9, 10 }; static long startTime, stopTime; public static void main(String[] args) { runImperative(); runFunctional(); runImperative(); runFunctional();runFunctional();runFunctional();runFunctional(); runImperative();runImperative();runImperative();runImperative(); runFunctional(); } private static void runFunctional() { startTime = System.nanoTime(); functionalApproach(); stopTime = System.nanoTime(); System.out.println("F:"+(stopTime - startTime)); } private static void runImperative() { startTime = System.nanoTime(); imperativeApproach(); stopTime = System.nanoTime(); System.out.println("I:"+(stopTime - startTime)); } private static void imperativeApproach() { int sum = 0; for (int j = 0; j < array.length; j++) { for (int k = j + 1; k < array.length; k++) { if (k != j && array[k] == array[j]) { sum = sum + array[k]; } } } } private static void functionalApproach() { IntStream.of(array).boxed().collect(Collectors.groupingBy(i -> i)).entrySet().stream() .filter(e -> e.getValue().size() > 1).forEach(e -> { e.getValue().stream().collect(Collectors.summingInt(i -> i)); }); } } here is my output ...

September 19, 2015 · 2 min · Özkan Pakdil