Skip to content

Commit

Permalink
Add StateT benchmark.
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasMikula committed Jun 3, 2016
1 parent 19cc2ba commit 0af73ad
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions bench/src/main/scala/cats/bench/StateTBench.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cats.bench

import cats.{Eval, Id, Monad}
import cats.data.StateT
import org.openjdk.jmh.annotations.{Benchmark, Scope, State}

@State(Scope.Benchmark)
class StateTBench {

def iteratedFlatMap[F[_]](n: Int)(implicit F: Monad[F]): StateT[F, Int, Int] =
(1 to n).foldLeft(StateT.pure[F, Int, Int](n))((st, _) => st.flatMap(i => StateT(_ => F.pure((i-1, i-1)))))

def recursiveFlatMap[F[_]](implicit F: Monad[F]): StateT[F, Int, Int] =
StateT[F, Int, Int](n => F.pure((n-1, n-1))).flatMap(i =>
if(i > 0) recursiveFlatMap
else StateT.pure[F, Int, Int](0)
)

@Benchmark
def strictIteratedFlatMap100: Int =
iteratedFlatMap[Id](100).runA(0)

@Benchmark
def strictIteratedFlatMap100k: Int =
iteratedFlatMap[Id](100000).runA(0)

@Benchmark
def strictRecursiveFlatMap100: Int =
recursiveFlatMap[Id].runA(100)

@Benchmark
def strictRecursiveFlatMap100k: Int =
recursiveFlatMap[Id].runA(100000)

@Benchmark
def trampolinedIteratedFlatMap100: Int =
iteratedFlatMap[Eval](100).runA(0).value

@Benchmark
def trampolinedIteratedFlatMap100k: Int =
iteratedFlatMap[Eval](100000).runA(0).value

@Benchmark
def trampolinedRecursiveFlatMap100: Int =
recursiveFlatMap[Eval].runA(100).value

@Benchmark
def trampolinedRecursiveFlatMap100k: Int =
recursiveFlatMap[Eval].runA(100000).value

}

0 comments on commit 0af73ad

Please sign in to comment.