-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #519 from non/topic/eval-call-bench
Set up a benchmark for our trampolines.
- Loading branch information
Showing
5 changed files
with
70 additions
and
6 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
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,45 @@ | ||
package cats.bench | ||
|
||
import org.openjdk.jmh.annotations.{Benchmark, Scope, State} | ||
|
||
import cats._ | ||
import cats.implicits._ | ||
import cats.free.Trampoline | ||
|
||
import scala.util.control.TailCalls | ||
|
||
@State(Scope.Benchmark) | ||
class TrampolineBench { | ||
|
||
val N = 15 | ||
|
||
@Benchmark | ||
def eval(): Int = evalFib(N).value | ||
|
||
def evalFib(n: Int): Eval[Int] = | ||
if (n < 2) Eval.now(n) else for { | ||
x <- Eval.defer(evalFib(n - 1)) | ||
y <- Eval.defer(evalFib(n - 2)) | ||
} yield x + y | ||
|
||
|
||
@Benchmark | ||
def trampoline(): Int = trampolineFib(N).run | ||
|
||
def trampolineFib(n: Int): Trampoline[Int] = | ||
if (n < 2) Trampoline.done(n) else for { | ||
x <- Trampoline.suspend(trampolineFib(n - 1)) | ||
y <- Trampoline.suspend(trampolineFib(n - 2)) | ||
} yield x + y | ||
|
||
// TailRec[A] only has .flatMap in 2.11. | ||
|
||
// @Benchmark | ||
// def stdlib(): Int = stdlibFib(N).result | ||
// | ||
// def stdlibFib(n: Int): TailCalls.TailRec[Int] = | ||
// if (n < 2) TailCalls.done(n) else for { | ||
// x <- TailCalls.tailcall(stdlibFib(n - 1)) | ||
// y <- TailCalls.tailcall(stdlibFib(n - 2)) | ||
// } yield x + y | ||
} |
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
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
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