Skip to content

Instantly share code, notes, and snippets.

@ktoso
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ktoso/facfcf8e0febfa6e6279 to your computer and use it in GitHub Desktop.
Save ktoso/facfcf8e0febfa6e6279 to your computer and use it in GitHub Desktop.
structural types are slow :-)
package com.typesafe.benchmarks
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
@State(Scope.Benchmark)
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
class ReflectBenchmark {
trait Cache {
def cache(a: AnyRef): AnyRef
}
class RealCache extends Cache {
def cache(a: AnyRef): AnyRef = a
}
class RealCache2 extends Cache {
def cache(a: AnyRef): AnyRef = a
}
class RealCache3 extends Cache {
def cache(a: AnyRef): AnyRef = a
}
type StructuralCache = {
def cache(a: AnyRef): AnyRef
}
val realCache = new RealCache
val realCache2 = new RealCache2
val realCache3 = new RealCache3
val cache: Cache = new RealCache
val structuralCache: StructuralCache = new RealCache
@Benchmark def direct = realCache.cache("")
@Benchmark def iface = cache.cache("")
@Benchmark def struct = structuralCache.cache("")
/*
Benchmark Mode Cnt Score Error Units
ReflectBenchmark.direct avgt 40 3.355 ± 0.184 ns/op
ReflectBenchmark.iface avgt 40 3.394 ± 0.090 ns/op
ReflectBenchmark.struct avgt 40 10.277 ± 0.894 ns/op
*/
}
@falconepl
Copy link

Just out of curiosity... Is this @Benchmark that you're using a wrapped ScalaMeter library utility underneath or some secret Typesafe's sauce for microbenchmarking (not available in public)?

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