Skip to content

Commit 1822b50

Browse files
committed
benchmark implicit anyval classes, and implicit functions
Result: nothing matters, it's all the same. ImplicitClassTest.testClassAnyVal avgt 10 3.156 ± 0.150 ns/op ImplicitClassTest.testClassStandard avgt 10 3.107 ± 0.124 ns/op ImplicitClassTest.testFunctionAnyVal avgt 10 3.159 ± 0.143 ns/op ImplicitClassTest.testFunctionStandard avgt 10 3.088 ± 0.047 ns/op
1 parent 095f229 commit 1822b50

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.openjdk.jmh.samples
2+
3+
import org.openjdk.jmh.annotations._
4+
5+
import java.util.concurrent.TimeUnit
6+
7+
object implicitClasses {
8+
9+
implicit final class ImplicitStandard[A](val oa: Option[A]) { def bar = oa.isDefined }
10+
implicit final class ImplicitAnyVal[A](val oa: Option[A]) extends AnyVal { def bar = oa.isDefined }
11+
12+
}
13+
14+
object implicitFunctions {
15+
16+
final class ImplicitStandard[A](val oa: Option[A]) { def bar = oa.isDefined }
17+
final class ImplicitAnyVal[A](val oa: Option[A]) extends AnyVal { def bar = oa.isDefined }
18+
19+
implicit def toStandard[A](oa: Option[A]) = new ImplicitStandard(oa)
20+
implicit def toAnyVal[A](oa: Option[A]) = new ImplicitAnyVal(oa)
21+
}
22+
23+
@State(Scope.Thread)
24+
@BenchmarkMode(Array(Mode.AverageTime))
25+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
26+
class ImplicitClassTest {
27+
28+
29+
val option = Option(42)
30+
31+
@Benchmark
32+
def testClassStandard = {
33+
import implicitClasses.ImplicitStandard
34+
option.bar
35+
}
36+
37+
@Benchmark
38+
def testClassAnyVal = {
39+
import implicitClasses.ImplicitAnyVal
40+
option.bar
41+
}
42+
43+
@Benchmark
44+
def testFunctionStandard = {
45+
import implicitFunctions.toStandard
46+
option.bar
47+
}
48+
49+
@Benchmark
50+
def testFunctionAnyVal = {
51+
import implicitFunctions.toAnyVal
52+
option.bar
53+
}
54+
}

0 commit comments

Comments
 (0)