Skip to content

Instantly share code, notes, and snippets.

@kailuowang
Created April 20, 2018 19:24
Show Gist options
  • Save kailuowang/61af69319b504955f4f4c6726bb0ba29 to your computer and use it in GitHub Desktop.
Save kailuowang/61af69319b504955f4f4c6726bb0ba29 to your computer and use it in GitHub Desktop.
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)
}
@kailuowang
Copy link
Author

results

[info] Benchmark                                       Mode  Cnt          Score        Error  Units
[info] timeSeries.bench.FooBench.map                  thrpt   15  166885391.415 ± 697350.415  ops/s
[info] timeSeries.bench.FooBench.mapSyntax            thrpt   15  151483900.409 ± 932694.508  ops/s
[info] timeSeries.bench.FooBench.reconstruct          thrpt   15  193632609.748 ± 819602.327  ops/s
[info] timeSeries.bench.FooBench.mapToString          thrpt   15   38809631.794 ± 511036.580  ops/s
[info] timeSeries.bench.FooBench.mapSyntaxToString    thrpt   15   80667615.131 ± 244852.441  ops/s
[info] timeSeries.bench.FooBench.reconstructToString  thrpt   15   45851849.614 ± 930765.674  ops/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment