| 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.ArrayBuffer |
| 16 |
|
| 17 |
object CountScalaTest |
| 18 |
{ |
| 19 |
private val SIZE = 1000000 |
| 20 |
private val integers = new ArrayBuffer[Int](SIZE) ++ (1 to SIZE) |
| 21 |
|
| 22 |
def megamorphic(megamorphicWarmupLevel: Int): Unit = |
| 23 |
{ |
| 24 |
if (megamorphicWarmupLevel > 0) |
| 25 |
{ |
| 26 |
// serial, lazy |
| 27 |
{ |
| 28 |
val evens = this.integers.view.count(_ % 2 == 0) |
| 29 |
Assert.assertEquals(SIZE / 2, evens) |
| 30 |
val odds = this.integers.view.count(_ % 2 == 1) |
| 31 |
Assert.assertEquals(SIZE / 2, odds) |
| 32 |
val evens2 = this.integers.view.count(each => (each & 1) == 0) |
| 33 |
Assert.assertEquals(SIZE / 2, evens2) |
| 34 |
} |
| 35 |
|
| 36 |
// parallel, lazy |
| 37 |
{ |
| 38 |
val evens = this.integers.par.count(_ % 2 == 0) |
| 39 |
Assert.assertEquals(SIZE / 2, evens) |
| 40 |
val odds = this.integers.par.count(_ % 2 == 1) |
| 41 |
Assert.assertEquals(SIZE / 2, odds) |
| 42 |
val evens2 = this.integers.par.count(each => (each & 1) == 0) |
| 43 |
Assert.assertEquals(SIZE / 2, evens2) |
| 44 |
} |
| 45 |
|
| 46 |
// serial, eager |
| 47 |
{ |
| 48 |
val evens = this.integers.count(_ % 2 == 0) |
| 49 |
Assert.assertEquals(SIZE / 2, evens) |
| 50 |
val odds = this.integers.count(_ % 2 == 1) |
| 51 |
Assert.assertEquals(SIZE / 2, odds) |
| 52 |
val evens2 = this.integers.count(each => (each & 1) == 0) |
| 53 |
Assert.assertEquals(SIZE / 2, evens2) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// deoptimize scala.collection.mutable.ResizableArray.foreach() |
| 58 |
// deoptimize scala.collection.IndexedSeqOptimized.foreach() |
| 59 |
if (megamorphicWarmupLevel > 1) |
| 60 |
{ |
| 61 |
this.integers.view.foreach(Assert.assertNotNull) |
| 62 |
this.integers.view.foreach(each => Assert.assertEquals(each, each)) |
| 63 |
this.integers.view.foreach(each => Assert.assertNotEquals(null, each)) |
| 64 |
this.integers.par.foreach(Assert.assertNotNull) |
| 65 |
this.integers.par.foreach(each => Assert.assertEquals(each, each)) |
| 66 |
this.integers.par.foreach(each => Assert.assertNotEquals(null, each)) |
| 67 |
} |
| 68 |
|
| 69 |
// deoptimize scala.collection.mutable.ResizableArray.foreach() |
| 70 |
// deoptimize scala.collection.IndexedSeqOptimized.foreach() |
| 71 |
if (megamorphicWarmupLevel > 2) |
| 72 |
{ |
| 73 |
this.integers.foreach(Assert.assertNotNull) |
| 74 |
this.integers.foreach(each => Assert.assertEquals(each, each)) |
| 75 |
this.integers.foreach(each => Assert.assertNotEquals(null, each)) |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
def serial_eager_scala(): Unit = |
| 80 |
{ |
| 81 |
val evens = this.integers.count(_ % 2 == 0) |
| 82 |
Assert.assertEquals(SIZE / 2, evens) |
| 83 |
} |
| 84 |
|
| 85 |
def serial_lazy_scala(): Unit = |
| 86 |
{ |
| 87 |
val evens = this.integers.view.count(_ % 2 == 0) |
| 88 |
Assert.assertEquals(SIZE / 2, evens) |
| 89 |
} |
| 90 |
|
| 91 |
def parallel_lazy_scala(): Unit = |
| 92 |
{ |
| 93 |
val evens = this.integers.par.count(_ % 2 == 0) |
| 94 |
Assert.assertEquals(SIZE / 2, evens) |
| 95 |
} |
| 96 |
} |