Created
April 20, 2018 19:24
-
-
Save kailuowang/61af69319b504955f4f4c6726bb0ba29 to your computer and use it in GitHub Desktop.
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
import cats.implicits._ | |
import implicits._ | |
import java.time.Instant | |
import cats.Functor | |
import org.openjdk.jmh.annotations.{Benchmark, Scope, State} | |
case class Foo[A](a: A) | |
object Foo { | |
implicit val functorFor : Functor[Foo] = new Functor[Foo] { | |
def map[A, B](fa: Foo[A])(f: A => B): Foo[B] = Foo(f(fa.a)) | |
} | |
} | |
@State(Scope.Benchmark) | |
class FooBench { | |
val foo = Foo(1) | |
def abstractFMap[F[_], B](fa: F[Int], f: Int => B)(implicit F: Functor[F]) = F.map(fa)(f) | |
@Benchmark | |
def mapSyntaxToString() = foo.map(_.toString) | |
@Benchmark | |
def mapToString() = abstractFMap(foo, _.toString) | |
@Benchmark | |
def reconstructToString() = Foo(foo.a.toString) | |
@Benchmark | |
def mapSyntax() = foo.map(_ + 1) | |
@Benchmark | |
def map() = abstractFMap(foo, _ + 1) | |
@Benchmark | |
def reconstruct() = Foo(foo.a + 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
results