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
-
Updated Kotlin to 1.4.21
-
Updated Kotlin to 1.5.30
Compiled with 11.0.12-zulu
lang | paradigm | variation | version | native | score |
---|---|---|---|---|---|
Groovy |
Imperative |
Inline |
17.0.0 |
false |
1079365370.88 |
Java |
Imperative |
Inline |
11.0.12 |
false |
1065646593.43 |
Kotlin |
Imperative |
Inline |
17.0.0 |
false |
911427265.49 |
Java |
Imperative |
Inline |
17.0.0 |
false |
907963943.99 |
Kotlin |
Imperative |
Inline |
11.0.12 |
false |
886100353.56 |
Groovy |
Imperative |
Inline |
11.0.12 |
false |
329062633.58 |
Groovy |
Imperative |
Inline |
21.2.0.r11-grl |
true |
25653958.86 |
Groovy |
Imperative |
Inline |
21.2.0.r16-grl |
true |
23737616.05 |
Java |
Imperative |
Inline |
21.2.0.r11-grl |
true |
22685656.48 |
Java |
Imperative |
Inline |
21.2.0.r16-grl |
true |
22505430.51 |
Kotlin |
Imperative |
Inline |
21.2.0.r11-grl |
true |
22251752.41 |
Kotlin |
Imperative |
Inline |
21.2.0.r16-grl |
true |
20902030.10 |
Java |
Imperative |
Call |
17.0.0 |
false |
1676706.83 |
Groovy |
Imperative |
Call |
21.2.0.r11-grl |
true |
1675809.86 |
Kotlin |
Imperative |
Call |
17.0.0 |
false |
1576954.02 |
Kotlin |
Imperative |
Call |
11.0.12 |
false |
1550893.14 |
Groovy |
Imperative |
Call |
17.0.0 |
false |
1548513.00 |
Scala |
Imperative |
Inline |
17.0.0 |
false |
1509134.61 |
Scala |
Imperative |
Inline |
11.0.12 |
false |
1484010.18 |
Groovy |
Imperative |
Call |
21.2.0.r16-grl |
true |
1330120.77 |
Java |
Imperative |
Call |
11.0.12 |
false |
1269404.86 |
Groovy |
Imperative |
Call |
11.0.12 |
false |
1215502.19 |
Kotlin |
Imperative |
Call |
21.2.0.r11-grl |
true |
1210708.72 |
Kotlin |
Imperative |
Call |
21.2.0.r16-grl |
true |
1129483.83 |
Java |
Imperative |
Call |
21.2.0.r16-grl |
true |
1115273.37 |
Java |
Imperative |
Call |
21.2.0.r11-grl |
true |
1113567.84 |
Scala |
Imperative |
Call |
21.2.0.r11-grl |
true |
1049351.24 |
Scala |
Imperative |
Inline |
21.2.0.r16-grl |
true |
950917.62 |
Scala |
Imperative |
Call |
21.2.0.r16-grl |
true |
948252.67 |
Scala |
Imperative |
Call |
11.0.12 |
false |
939091.14 |
Scala |
Imperative |
Inline |
21.2.0.r11-grl |
true |
934453.73 |
Scala |
Imperative |
Call |
17.0.0 |
false |
872538.58 |
Kotlin |
Functional |
17.0.0 |
false |
591111.01 |
|
Scala |
Functional |
21.2.0.r11-grl |
true |
585884.63 |
|
Scala |
Functional |
21.2.0.r16-grl |
true |
573594.12 |
|
Scala |
Functional |
Z |
21.2.0.r16-grl |
true |
540049.84 |
Scala |
Functional |
Z |
21.2.0.r11-grl |
true |
519120.36 |
Scala |
Functional |
Z |
17.0.0 |
false |
500733.95 |
Kotlin |
Functional |
21.2.0.r11-grl |
true |
498982.69 |
|
Kotlin |
Functional |
11.0.12 |
false |
461508.87 |
|
Kotlin |
Functional |
21.2.0.r16-grl |
true |
424752.37 |
|
Scala |
Functional |
17.0.0 |
false |
393387.51 |
|
Scala |
Functional |
Z |
11.0.12 |
false |
385908.46 |
Scala |
Functional |
11.0.12 |
false |
342851.61 |
|
Kotlin |
Functional |
MapReduce |
17.0.0 |
false |
296822.68 |
Java |
Functional |
Vavr |
21.2.0.r11-grl |
true |
283806.49 |
Kotlin |
Functional |
MapReduce |
21.2.0.r16-grl |
true |
272830.50 |
Kotlin |
Functional |
MapReduce |
21.2.0.r11-grl |
true |
269854.93 |
Java |
Functional |
Vavr |
21.2.0.r16-grl |
true |
255897.56 |
Kotlin |
Functional |
MapReduce |
11.0.12 |
false |
205219.08 |
Java |
Functional |
Vavr |
17.0.0 |
false |
196162.17 |
Java |
Functional |
Vavr |
11.0.12 |
false |
186002.45 |
Java |
Functional |
21.2.0.r11-grl |
true |
131690.10 |
|
Java |
Functional |
21.2.0.r16-grl |
true |
129849.56 |
|
Java |
Functional |
11.0.12 |
false |
100824.82 |
|
Java |
Functional |
MapReduce |
21.2.0.r11-grl |
true |
97713.64 |
Java |
Functional |
MapReduce |
21.2.0.r16-grl |
true |
79576.24 |
Java |
Functional |
MapReduce |
11.0.12 |
false |
73576.92 |
Scala |
Functional |
Streams |
21.2.0.r11-grl |
true |
73150.65 |
Java |
Functional |
MapReduce |
17.0.0 |
false |
65023.44 |
Java |
Functional |
17.0.0 |
false |
64580.80 |
|
Scala |
Functional |
Streams |
11.0.12 |
false |
62689.24 |
Scala |
Functional |
Streams |
17.0.0 |
false |
53379.36 |
Scala |
Functional |
Streams |
21.2.0.r16-grl |
true |
43179.79 |
Groovy |
Functional |
21.2.0.r11-grl |
true |
18562.48 |
|
Groovy |
Functional |
17.0.0 |
false |
12923.49 |
|
Groovy |
Functional |
21.2.0.r16-grl |
true |
12235.32 |
|
Groovy |
Functional |
11.0.12 |
false |
11233.63 |
lang | paradigm | variation | version | native | score | changed | improvement |
---|---|---|---|---|---|---|---|
Groovy |
Functional |
11.0.12 |
FALSE |
14917.33 |
11233.63 |
75.31% |
|
Groovy |
Functional |
17.0.0 |
FALSE |
14844.68 |
12923.49 |
87.06% |
|
Groovy |
Functional |
21.2.0.r11-grl |
TRUE |
11274.00 |
18562.48 |
164.65% |
|
Groovy |
Functional |
21.2.0.r16-grl |
TRUE |
13382.11 |
12235.32 |
91.43% |
|
Groovy |
Imperative |
Call |
11.0.12 |
FALSE |
1433779.55 |
1215502.19 |
84.78% |
Groovy |
Imperative |
Call |
17.0.0 |
FALSE |
1733365.84 |
1548513 |
89.34% |
Groovy |
Imperative |
Call |
21.2.0.r11-grl |
TRUE |
1098518.23 |
1675809.86 |
152.55% |
Groovy |
Imperative |
Call |
21.2.0.r16-grl |
TRUE |
1733579.10 |
1330120.77 |
76.73% |
Groovy |
Imperative |
Inline |
11.0.12 |
FALSE |
400855589.35 |
329062633.6 |
82.09% |
Groovy |
Imperative |
Inline |
17.0.0 |
FALSE |
1097306196.76 |
1079365371 |
98.37% |
Groovy |
Imperative |
Inline |
21.2.0.r11-grl |
TRUE |
22207162.06 |
25653958.86 |
115.52% |
Groovy |
Imperative |
Inline |
21.2.0.r16-grl |
TRUE |
1318384796.13 |
23737616.05 |
1.80% |
Java |
Functional |
11.0.12 |
FALSE |
94122.95 |
73576.92 |
78.17% |
|
Java |
Functional |
17.0.0 |
FALSE |
62846.48 |
65023.44 |
103.46% |
|
Java |
Functional |
21.2.0.r11-grl |
TRUE |
97761.83 |
97713.64 |
99.95% |
|
Java |
Functional |
21.2.0.r16-grl |
TRUE |
66559.06 |
79576.24 |
119.56% |
|
Java |
Functional |
MapReduce |
11.0.12 |
FALSE |
88548.93 |
186002.45 |
210.06% |
Java |
Functional |
MapReduce |
17.0.0 |
FALSE |
78297.56 |
196162.17 |
250.53% |
Java |
Functional |
MapReduce |
21.2.0.r11-grl |
TRUE |
73929.35 |
283806.49 |
383.89% |
Java |
Functional |
MapReduce |
21.2.0.r16-grl |
TRUE |
85129.62 |
255897.56 |
300.60% |
Java |
Functional |
Vavr |
11.0.12 |
FALSE |
217079.50 |
100824.82 |
46.45% |
Java |
Functional |
Vavr |
17.0.0 |
FALSE |
229655.15 |
64580.8 |
28.12% |
Java |
Functional |
Vavr |
21.2.0.r11-grl |
TRUE |
196451.01 |
131690.1 |
67.03% |
Java |
Functional |
Vavr |
21.2.0.r16-grl |
TRUE |
231999.41 |
129849.56 |
55.97% |
Java |
Imperative |
Call |
11.0.12 |
FALSE |
1833616.28 |
1269404.86 |
69.23% |
Java |
Imperative |
Call |
17.0.0 |
FALSE |
1786868.25 |
1676706.83 |
93.83% |
Java |
Imperative |
Call |
21.2.0.r11-grl |
TRUE |
907217.63 |
1113567.84 |
122.75% |
Java |
Imperative |
Call |
21.2.0.r16-grl |
TRUE |
2015751.27 |
1115273.37 |
55.33% |
Java |
Imperative |
Inline |
11.0.12 |
FALSE |
1185278723.82 |
1065646593 |
89.91% |
Java |
Imperative |
Inline |
17.0.0 |
FALSE |
996876475.82 |
907963944 |
91.08% |
Java |
Imperative |
Inline |
21.2.0.r11-grl |
TRUE |
16960141.84 |
22685656.48 |
133.76% |
Java |
Imperative |
Inline |
21.2.0.r16-grl |
TRUE |
1315067555.98 |
22505430.51 |
1.71% |
Kotlin |
Functional |
11.0.12 |
FALSE |
612559.76 |
205219.08 |
33.50% |
|
Kotlin |
Functional |
17.0.0 |
FALSE |
635630.46 |
296822.68 |
46.70% |
|
Kotlin |
Functional |
21.2.0.r11-grl |
TRUE |
329871.92 |
269854.93 |
81.81% |
|
Kotlin |
Functional |
21.2.0.r16-grl |
TRUE |
751525.10 |
272830.5 |
36.30% |
|
Kotlin |
Functional |
MapReduce |
11.0.12 |
FALSE |
243119.80 |
461508.87 |
189.83% |
Kotlin |
Functional |
MapReduce |
17.0.0 |
FALSE |
308806.19 |
591111.01 |
191.42% |
Kotlin |
Functional |
MapReduce |
21.2.0.r11-grl |
TRUE |
210542.73 |
498982.69 |
237.00% |
Kotlin |
Functional |
MapReduce |
21.2.0.r16-grl |
TRUE |
326162.74 |
424752.37 |
130.23% |
Kotlin |
Imperative |
Call |
11.0.12 |
FALSE |
1735967.08 |
1550893.14 |
89.34% |
Kotlin |
Imperative |
Call |
17.0.0 |
FALSE |
1573645.87 |
1576954.02 |
100.21% |
Kotlin |
Imperative |
Call |
21.2.0.r11-grl |
TRUE |
1217797.04 |
1210708.72 |
99.42% |
Kotlin |
Imperative |
Call |
21.2.0.r16-grl |
TRUE |
1789109.46 |
1129483.83 |
63.13% |
Kotlin |
Imperative |
Inline |
11.0.12 |
FALSE |
608710261.31 |
886100353.6 |
145.57% |
Kotlin |
Imperative |
Inline |
17.0.0 |
FALSE |
903619032.64 |
911427265.5 |
100.86% |
Kotlin |
Imperative |
Inline |
21.2.0.r11-grl |
TRUE |
23850637.36 |
22251752.41 |
93.30% |
Kotlin |
Imperative |
Inline |
21.2.0.r16-grl |
TRUE |
1323461944.89 |
20902030.1 |
1.58% |
Scala |
Functional |
11.0.12 |
FALSE |
247764.35 |
62689.24 |
25.30% |
|
Scala |
Functional |
17.0.0 |
FALSE |
405825.23 |
53379.36 |
13.15% |
|
Scala |
Functional |
21.2.0.r11-grl |
TRUE |
574252.49 |
73150.65 |
12.74% |
|
Scala |
Functional |
21.2.0.r16-grl |
TRUE |
414104.57 |
43179.79 |
10.43% |
|
Scala |
Functional |
Streams |
11.0.12 |
FALSE |
46519.51 |
385908.46 |
829.56% |
Scala |
Functional |
Streams |
17.0.0 |
FALSE |
53011.68 |
500733.95 |
944.57% |
Scala |
Functional |
Streams |
21.2.0.r11-grl |
TRUE |
67392.01 |
519120.36 |
770.30% |
Scala |
Functional |
Streams |
21.2.0.r16-grl |
TRUE |
60384.77 |
540049.84 |
894.35% |
Scala |
Functional |
Z |
11.0.12 |
FALSE |
349362.94 |
342851.61 |
98.14% |
Scala |
Functional |
Z |
17.0.0 |
FALSE |
457628.71 |
393387.51 |
85.96% |
Scala |
Functional |
Z |
21.2.0.r11-grl |
TRUE |
574676.30 |
585884.63 |
101.95% |
Scala |
Functional |
Z |
21.2.0.r16-grl |
TRUE |
493710.13 |
573594.12 |
116.18% |
Scala |
Imperative |
Call |
11.0.12 |
FALSE |
843812.50 |
939091.14 |
111.29% |
Scala |
Imperative |
Call |
17.0.0 |
FALSE |
699225.33 |
872538.58 |
124.79% |
Scala |
Imperative |
Call |
21.2.0.r11-grl |
TRUE |
1063185.85 |
1049351.24 |
98.70% |
Scala |
Imperative |
Call |
21.2.0.r16-grl |
TRUE |
1212173.35 |
948252.67 |
78.23% |
Scala |
Imperative |
Inline |
11.0.12 |
FALSE |
1447523.98 |
1484010.18 |
102.52% |
Scala |
Imperative |
Inline |
17.0.0 |
FALSE |
1803322.32 |
1509134.61 |
83.69% |
Scala |
Imperative |
Inline |
21.2.0.r11-grl |
TRUE |
1190108.18 |
934453.73 |
78.52% |
Scala |
Imperative |
Inline |
21.2.0.r16-grl |
TRUE |
2010448.88 |
950917.62 |
47.30% |