During a recent talk on programming paradigms [Pavel Tcholakov](https://twitter.com/pavletko) presented an example using Java 8 streams to solve the classic FizzBuzz problem.
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
Here is a little project to compare the performance of imperative and functional programming.
The [jmh gradle plugin](https://github.com/melix/jmh-gradle-plugin) was a great discovery.
The numbers on Intel CoreTM i7 2.9Ghz
-
Added [Scala](http://www.scala-lang.org) versions.
-
Added [Groovy](http://www.groovy-lang.org) versions.
-
Created Java functional that doesn’t perform map/reduce.
-
Refactored Scala version to similar functional without map/reduce.
-
Added [Kotlin](https://kotlinlang.org) versions
-
Added Scala Streams after [Guillaume Belrose](https://twitter.com/gbelrose) linked to [https://gist.github.com/samklr/6098769](https://gist.github.com/samklr/6098769)
-
Refactored Java and Scala Predicates to inline lambdas instead of static methods. No significant difference in performance.
-
Added Scalaz version after [Andreas Pauley](https://twitter.com/apauley) linked to [https://gist.github.com/apauley/ca8197f5b9b822097c50](https://gist.github.com/apauley/ca8197f5b9b822097c50) Providing some of the best performance of all the functional versions
-
Kotlin 1.1.3 moves up in the functional performance.
-
Kotlin 1.2.10 improves functional performance.
-
Added Java - Vavr using vavr 0.9.2.
-
Updated Kotlin to 1.2.41.
-
Updated Kotlin to 1.3.21
-
Updated Kotlin to 1.3.50
-
Updated Scala 2.13
Units = ops/s JVM = GraalVM 19.0.2 ** Zulu 11.0.3 — Faster for inline functions and some imperative calls
Benchmark Mode Cnt Score Error Units
FixBuzzBenchmark.testGroovyFunctional thrpt 5 14033.643 ± 275.001 ops/s
FixBuzzBenchmark.testGroovyImperativeCall thrpt 5 692425.786 ± 10268.653 ops/s
FixBuzzBenchmark.testGroovyImperativeInline thrpt 5 2058149.075 ± 204481.705 ops/s
FixBuzzBenchmark.testJavaFunctionalMapReduce thrpt 5 95093.850 ± 2874.418 ops/s
FixBuzzBenchmark.testJavaFunctional thrpt 5 107296.501 ± 5665.556 ops/s
FixBuzzBenchmark.testJavaFunctionalVavr thrpt 5 159015.498 ± 8205.382 ops/s
FixBuzzBenchmark.testJavaImperativeCall thrpt 5 812832.030 ± 7865.445 ops/s
FixBuzzBenchmark.testJavaImperativeInline thrpt 5 2704456.448 ± 21528.660 ops/s
FixBuzzBenchmark.testKotlinFunctionalMapReduce thrpt 5 189833.971 ± 4207.546 ops/s
FixBuzzBenchmark.testKotlinFunctional thrpt 5 555922.116 ± 8437.670 ops/s
FixBuzzBenchmark.testKotlinImperativeCall thrpt 5 812925.556 ± 5785.430 ops/s
FixBuzzBenchmark.testKotlinImperativeInline thrpt 5 4926570.829 ± 74741.546 ops/s
FixBuzzBenchmark.testScalaFunctionalStreams thrpt 5 70654.437 ± 2765.868 ops/s
FixBuzzBenchmark.testScalaFunctional thrpt 5 265563.027 ± 16636.111 ops/s
FixBuzzBenchmark.testScalaZFunctional thrpt 5 359192.054 ± 6148.299 ops/s
FixBuzzBenchmark.testScalaImperativeInline thrpt 5 605244.240 ± 17440.370 ops/s
FixBuzzBenchmark.testScalaImperativeCall thrpt 5 776830.120 ± 10062.794 ops/s
Benchmark Mode Cnt Score
FixBuzzBenchmark.testScalaImperativeInline thrpt 5 605244.240
FixBuzzBenchmark.testGroovyImperativeCall thrpt 5 692425.786
FixBuzzBenchmark.testScalaImperativeCall thrpt 5 776830.120
FixBuzzBenchmark.testJavaImperativeCall thrpt 5 812832.030
FixBuzzBenchmark.testKotlinImperativeCall thrpt 5 812925.556
FixBuzzBenchmark.testGroovyImperativeInline thrpt 5 2058149.075
FixBuzzBenchmark.testJavaImperativeInline thrpt 5 2704456.448
FixBuzzBenchmark.testKotlinImperativeInline thrpt 5 4926570.829
Benchmark Mode Cnt Score
FixBuzzBenchmark.testGroovyFunctional thrpt 5 14033.643
FixBuzzBenchmark.testScalaFunctionalStreams thrpt 5 70654.437
FixBuzzBenchmark.testJavaFunctionalMapReduce thrpt 5 95093.850
FixBuzzBenchmark.testJavaFunctional thrpt 5 107296.501
FixBuzzBenchmark.testJavaFunctionalVavr thrpt 5 159015.498
FixBuzzBenchmark.testKotlinFunctionalMapReduce thrpt 5 189833.971
FixBuzzBenchmark.testScalaFunctional thrpt 5 265563.027
FixBuzzBenchmark.testScalaZFunctional thrpt 5 359192.054
FixBuzzBenchmark.testKotlinFunctional thrpt 5 555922.116
Benchmark Mode Cnt Score Error Units
FixBuzzBenchmark.testGroovyFunctional thrpt 5 13011.848 ± 2208.804 ops/s
FixBuzzBenchmark.testGroovyImperativeCall thrpt 5 1594374.931 ± 325836.986 ops/s
FixBuzzBenchmark.testGroovyImperativeInline thrpt 5 345634876.023 ± 14352777.499 ops/s
FixBuzzBenchmark.testJavaFunctionalMapReduce thrpt 5 91072.952 ± 13995.374 ops/s
FixBuzzBenchmark.testJavaFunctional thrpt 5 91501.518 ± 3638.998 ops/s
FixBuzzBenchmark.testJavaFunctionalVavr thrpt 5 188234.360 ± 57944.488 ops/s
FixBuzzBenchmark.testJavaImperativeCall thrpt 5 1619649.802 ± 260137.707 ops/s
FixBuzzBenchmark.testJavaImperativeInline thrpt 5 1181872487.203 ± 297701783.059 ops/s
FixBuzzBenchmark.testKotlinFunctionalMapReduce thrpt 5 282298.530 ± 10856.408 ops/s
FixBuzzBenchmark.testKotlinFunctional thrpt 5 834214.699 ± 87449.009 ops/s
FixBuzzBenchmark.testKotlinImperativeCall thrpt 5 1714984.169 ± 118376.587 ops/s
FixBuzzBenchmark.testKotlinImperativeInline thrpt 5 1215417853.550 ± 112955920.457 ops/s
FixBuzzBenchmark.testScalaFunctionalStreams thrpt 5 77724.817 ± 6395.448 ops/s
FixBuzzBenchmark.testScalaFunctional thrpt 5 307556.782 ± 32169.705 ops/s
FixBuzzBenchmark.testScalaZFunctional thrpt 5 442720.769 ± 34878.548 ops/s
FixBuzzBenchmark.testScalaImperativeCall thrpt 5 1780622.740 ± 177143.124 ops/s
FixBuzzBenchmark.testScalaImperativeInline thrpt 5 1866152.071 ± 191462.125 ops/s