-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathStreamBench.scala
101 lines (87 loc) · 3.19 KB
/
StreamBench.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package benchmarks
import java.util.concurrent.TimeUnit
import org.openjdk.jmh.annotations._
import scalaz.{IList, EphemeralStream}
import scalaz.Scalaz._
// --- //
@BenchmarkMode(Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@State(Scope.Thread)
class StreamBench {
var list1: List[Int] = _
var list2: List[Int] = _
var ilist1: IList[Int] = _
var ilist2: IList[Int] = _
var vec1: Vector[Int] = _
var vec2: Vector[Int] = _
var arr1: Array[Int] = _
var arr2: Array[Int] = _
var str1: Stream[Int] = _
var str2: Stream[Int] = _
var estr1: EphemeralStream[Int] = _
var estr2: EphemeralStream[Int] = _
@Setup
def setup: Unit = {
list1 = List.range(1, 10000)
list2 = List.range(10000, 1, -1)
ilist1 = IList.fromList(list1)
ilist2 = IList.fromList(list2)
vec1 = Vector.range(1, 10000)
vec2 = Vector.range(10000, 1, -1)
arr1 = Array.range(1, 10000)
arr2 = Array.range(10000, 1, -1)
str1 = Stream.range(1, 10000)
str2 = Stream.range(10000, 1, -1)
estr1 = EphemeralStream.range(1, 10000)
}
@Benchmark
def streamMax: Int = str1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).max
@Benchmark
def streamHead: Int = str1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).head
@Benchmark
def streamReverse: Int = str1.reverse.head
@Benchmark
def streamSort: Int = str2.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).sorted.head
@Benchmark
def ephemeralStreamMax: Option[Int] = estr1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).maximum
@Benchmark
def ephemeralStreamHead: Option[Int] = estr1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).headOption
@Benchmark
def ephemeralStreamReverse: Option[Int] = estr1.reverse.headOption
@Benchmark
def iterMax: Int = list1.iterator.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).max
@Benchmark
def iterHead: Int = list1.iterator.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).next
@Benchmark
def listMax: Int = list1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).max
@Benchmark
def listHead: Int = list1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).head
@Benchmark
def listReverse: Int = list1.reverse.head
@Benchmark
def listSort: Int = list2.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).sorted.head
@Benchmark
def ilistMax: Option[Int] = ilist1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).maximum
@Benchmark
def ilistHead: Option[Int] = ilist1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).headOption
@Benchmark
def ilistReverse: Option[Int] = ilist1.reverse.headOption
@Benchmark
def ilistSort: Option[Int] = ilist2.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).sorted.headOption
@Benchmark
def vectorMax: Int = vec1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).max
@Benchmark
def vectorHead: Int = vec1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).head
@Benchmark
def vectorReverse: Int = vec1.reverse.head
@Benchmark
def vectorSort: Int = vec2.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).sorted.head
@Benchmark
def arrayMax: Int = arr1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).max
@Benchmark
def arrayHead: Int = arr1.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).head
@Benchmark
def arrayReverse: Int = arr1.reverse.head
@Benchmark
def arraySort: Int = arr2.map(_ + 1).filter(_ % 2 == 0).map(_ * 2).sorted.head
}