Last active
August 29, 2015 14:16
-
-
Save ktoso/facfcf8e0febfa6e6279 to your computer and use it in GitHub Desktop.
structural types are slow :-)
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
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 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)?