forked from typelevel/cats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
19cc2ba
commit 0af73ad
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |