Debian Codegraph @

jmh-scala-tests/src/main/scala/org/eclipse/collections/impl/jmh/FunctionalInterfaceScalaTest.scala

raw
1 /*
2 * Copyright (c) 2021 Goldman Sachs.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * and Eclipse Distribution License v. 1.0 which accompany this distribution.
6 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7 * and the Eclipse Distribution License is available at
8 * http://www.eclipse.org/org/documents/edl-v10.php.
9 */
10
11 package org.eclipse.collections.impl.jmh
12
13 import org.junit.Assert
14
15 import scala.collection.mutable
16 import scala.collection.mutable.ArrayBuffer
17
18 object FunctionalInterfaceScalaTest
19 {
20 private val SIZE: Int = 1000000
21 private val integers: ArrayBuffer[Int] = new ArrayBuffer[Int]() ++ (1 to SIZE)
22
23 def megamorphic(megamorphicWarmupLevel: Int)
24 {
25 val predicate1: (Int) => Boolean = each => (each + 2) % 10000 != 0
26 val predicate2: (Int) => Boolean = each => (each + 3) % 10000 != 0
27 val predicate3: (Int) => Boolean = each => (each + 4) % 10000 != 0
28 val predicate4: (Int) => Boolean = each => (each + 5) % 10000 != 0
29
30 val function1: (Int) => String = each =>
31 {
32 Assert.assertNotNull(each)
33 String.valueOf(each)
34 }
35
36 val function2: (String) => Int = each =>
37 {
38 Assert.assertNotNull(each)
39 Integer.valueOf(each)
40 }
41
42 val function3: (Int) => String = each =>
43 {
44 Assert.assertEquals(each, each)
45 String.valueOf(each)
46 }
47
48 val function4: (String) => Int = each =>
49 {
50 Assert.assertEquals(each, each)
51 Integer.valueOf(each)
52 }
53
54 if (megamorphicWarmupLevel > 0)
55 {
56 // serial, lazy, EC
57 {
58 val set = this.integers.view.filter(predicate1).map(function1).map(function2).filter(predicate2).toSet
59 Assert.assertEquals(999800, set.size)
60 val buffer = this.integers.view.filter(predicate3).map(function3).map(function4).filter(predicate4).toBuffer
61 Assert.assertEquals(999800, buffer.size)
62 }
63
64 // parallel, lazy, EC
65 {
66 val set = this.integers.par.filter(predicate1).map(function1).map(function2).filter(predicate2).toSet
67 Assert.assertEquals(999800, set.size)
68 val buffer = this.integers.par.filter(predicate3).map(function3).map(function4).filter(predicate4).toBuffer
69 Assert.assertEquals(999800, buffer.size)
70 }
71
72 // serial, eager, EC
73 {
74 val set = this.integers.filter(predicate1).map(function1).map(function2).filter(predicate2).toSet
75 Assert.assertEquals(999800, set.size)
76 val buffer = this.integers.filter(predicate3).map(function3).map(function4).filter(predicate4).toBuffer
77 Assert.assertEquals(999800, buffer.size)
78 }
79 }
80 }
81
82 def serial_eager_scala(): ArrayBuffer[Integer] =
83 {
84 val list = this.integers
85 .filter(each => each % 10000 != 0)
86 .map(String.valueOf)
87 .map(Integer.valueOf)
88 .filter(each => (each + 1) % 10000 != 0)
89 Assert.assertEquals(999800, list.size)
90 list
91 }
92
93 def test_serial_eager_scala()
94 {
95 Assert.assertEquals(1.to(1000000, 10000).flatMap(each => each.to(each + 9997)).toBuffer, this.serial_eager_scala())
96 }
97
98 def serial_lazy_scala(): mutable.Buffer[Integer] =
99 {
100 val list = this.integers
101 .view
102 .filter(each => each % 10000 != 0)
103 .map(String.valueOf)
104 .map(Integer.valueOf)
105 .filter(each => (each + 1) % 10000 != 0)
106 .toBuffer
107 Assert.assertEquals(999800, list.size)
108 list
109 }
110
111 def test_serial_lazy_scala()
112 {
113 Assert.assertEquals(1.to(1000000, 10000).flatMap(each => each.to(each + 9997)).toBuffer, this.serial_lazy_scala())
114 }
115
116 def parallel_lazy_scala(): mutable.Buffer[Integer] =
117 {
118 val list = this.integers
119 .par
120 .filter(each => each % 10000 != 0)
121 .map(String.valueOf)
122 .map(Integer.valueOf)
123 .filter(each => (each + 1) % 10000 != 0)
124 .toBuffer
125 Assert.assertEquals(999800, list.size)
126 list
127 }
128
129 def test_parallel_lazy_scala()
130 {
131 Assert.assertEquals(1.to(1000000, 10000).flatMap(each => each.to(each + 9997)).toBuffer, this.parallel_lazy_scala())
132 }
133
134 def parallel_lazy_scala_hand_coded(): mutable.Buffer[Int] =
135 {
136 val list = this.integers
137 .par
138 .filter(integer => integer % 10000 != 0 && (Integer.valueOf(String.valueOf(integer)) + 1) % 10000 != 0)
139 .toBuffer
140 Assert.assertEquals(999800, list.size)
141 list
142 }
143
144 def test_parallel_lazy_scala_hand_coded()
145 {
146 Assert.assertEquals(1.to(1000000, 10000).flatMap(each => each.to(each + 9997)).toBuffer, this.parallel_lazy_scala_hand_coded())
147 }
148 }