Skip to content

Comparison of Imperative and Functional Programming performance

Notifications You must be signed in to change notification settings

kabutz/compare-fp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Comparing Functional and Imperative performance

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

Changelog

Results

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

Imperative

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

Functional

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

Java 12.0.1

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


Buy Me a Coffee at ko-fi.com

About

Comparison of Imperative and Functional Programming performance

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 71.8%
  • Scala 10.5%
  • Kotlin 10.0%
  • Groovy 7.7%