Skip to content

Commit cb95383

Browse files
Add boxing benchmark for report
1 parent b1e16ab commit cb95383

File tree

1 file changed

+27
-0
lines changed
  • bench-micro/src/main/scala/dotty/tools/benchmarks/inlinetraits

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package dotty.tools.benchmarks.inlinetraits
2+
3+
import org.openjdk.jmh.annotations._
4+
import java.util.concurrent.TimeUnit.{SECONDS, MILLISECONDS}
5+
import scala.util.Random
6+
7+
// @BenchmarkMode(Array(Mode.SampleTime))
8+
@Fork(3)
9+
@Threads(3)
10+
@Warmup(iterations = 5, time = 10, timeUnit = SECONDS)
11+
@Measurement(iterations = 10, time = 10, timeUnit = SECONDS)
12+
// @OutputTimeUnit(MILLISECONDS)
13+
@State(Scope.Benchmark)
14+
class Boxing {
15+
class Wrapper[T](val x: T)
16+
class IntWrapper(val x: Int)
17+
18+
val numbers = 1 to 1000000
19+
val ws = numbers.map(Wrapper(_))
20+
val iws = numbers.map(IntWrapper(_))
21+
22+
@Benchmark
23+
def noUnboxing: Int = iws.map(w => IntWrapper(w.x*w.x + 2*w.x)).foldLeft(0)(_ + _.x)
24+
25+
@Benchmark
26+
def withUnboxing: Int = ws.map(w => Wrapper(w.x*w.x + 2*w.x)).foldLeft(0)(_ + _.x)
27+
}

0 commit comments

Comments
 (0)