Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Created November 21, 2017 20:32
Show Gist options
  • Save Jacoby6000/ff49c09f2255c50e255f9eaff7531a68 to your computer and use it in GitHub Desktop.
Save Jacoby6000/ff49c09f2255c50e255f9eaff7531a68 to your computer and use it in GitHub Desktop.
package scalaz.effect
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import scala.concurrent.Await
import IOBenchmarks._
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class IODeepFlatMapBenchmark {
@Param(Array("20"))
var depth: Int = _
@Benchmark
def futureDeepFlatMap(): BigInt = {
import scala.concurrent.Future
import scala.concurrent.duration.Duration.Inf
def fib(n: Int): Future[BigInt] =
if (n <= 1) Future.successful(n) else
fib(n-1).flatMap { a =>
fib(n-2).flatMap(b => Future.successful(a + b))
}
Await.result(fib(depth), Inf)
}
@Benchmark
def scalazDeepFlatMap(): BigInt = {
def fib(n: Int): IO[BigInt] =
if (n <= 1) IO.now(n) else
fib(n-1).flatMap { a =>
fib(n-2).flatMap(b => IO.now(a + b))
}
unsafePerformIO(fib(depth))
}
}
[info] Benchmark (depth) Mode Cnt Score Error Units
[info] IODeepFlatMapBenchmark.futureDeepFlatMap 20 thrpt 200 23.213 ± 0.523 ops/s
[info] IODeepFlatMapBenchmark.scalazDeepFlatMap 20 thrpt 200 1390.392 ± 59.044 ops/s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment