-
Notifications
You must be signed in to change notification settings - Fork 0
/
CatsTraverseWithIndex.scala
46 lines (38 loc) · 1.05 KB
/
CatsTraverseWithIndex.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//> using scala 3.3.1
//> using toolkit typelevel:latest
package bench
import cats.syntax.all.*
import org.openjdk.jmh.annotations.*
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.infra.Blackhole
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
@Measurement(iterations = 15, timeUnit = TimeUnit.SECONDS, time = 3)
@Warmup(iterations = 15, timeUnit = TimeUnit.SECONDS, time = 3)
@Fork(3)
@Threads(value = 1)
class CatsTraverseWithIndex:
private[this] val Work: Long = 2
@Param(Array("50", "1000", "100000", "10000000"))
var size: Int = _
var xs: List[Int] = _
@Setup
def setup =
xs = (1 to size).toList
@Benchmark
def traverseWithIndexM =
xs.traverseWithIndexM: (x, i) =>
Blackhole.consumeCPU(Work)
(x + i).some
@Benchmark
def zipMapSequence =
xs.zipWithIndex.map: (x, i) =>
Blackhole.consumeCPU(Work)
(x + i).some
.sequence
@Benchmark
def zipTraverse =
xs.zipWithIndex.traverse: (x, i) =>
Blackhole.consumeCPU(Work)
(x + i).some