Skip to content

Commit

Permalink
GroupMap benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Jan 3, 2022
1 parent 0a095be commit d8bf672
Showing 1 changed file with 182 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package scala.collection

import java.util.concurrent.TimeUnit

import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole


@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 8)
@Measurement(iterations = 8)
@State(Scope.Benchmark)
class GroupMapBenchmark {

type CC[A] = immutable.List[A]
val factory = immutable.List

@Param(scala.Array("2", "3", "5", "16", "17", "32", "33", "128", "129"))
var size: Int = _

var xs: CC[Long] = _

def fresh(n: Int) = factory((1 to n).map(_.toLong): _*)

@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(10000)
}

@Benchmark
def groupMap(bh: Blackhole): Unit =
xs.groupMap(_ % size)(_ * 2).foreach(bh.consume)

}

@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 8)
@Measurement(iterations = 8)
@State(Scope.Benchmark)
class GroupMapValuesBenchmark {

type CC[A] = immutable.List[A]
val factory = immutable.List

@Param(scala.Array("2", "3", "5", "16", "17", "32", "33", "128", "129"))
var size: Int = _

var xs: CC[Long] = _

def fresh(n: Int) = factory((1 to n).map(_.toLong): _*)

@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(10000)
}

@Benchmark
def groupMap(bh: Blackhole): Unit =
xs.groupBy(_ % size).mapValues(_.map(_ * 2)).foreach(bh.consume)

}

@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 8)
@Measurement(iterations = 8)
@State(Scope.Benchmark)
class ScalaGroupMapValuesBenchmark {

type CC[A] = scala.collection.immutable.List[A]
val factory = scala.collection.immutable.List

@Param(scala.Array("2", "3", "5", "16", "17", "32", "33", "128", "129"))
var size: Int = _

var xs: CC[Long] = _

def fresh(n: Int) = factory((1 to n).map(_.toLong): _*)

@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(10000)
}

@Benchmark
def groupMap(bh: Blackhole): Unit =
xs.groupBy(_ % size).mapValues(_.map(_ * 2)).foreach(bh.consume)

}

@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 8)
@Measurement(iterations = 8)
@State(Scope.Benchmark)
class GroupMapReduceBenchmark {

type CC[A] = immutable.List[A]
val factory = immutable.List

@Param(scala.Array("2", "3", "5", "16", "17", "32", "33", "128", "129"))
var size: Int = _

var xs: CC[Long] = _

def fresh(n: Int) = factory((1 to n).map(_.toLong): _*)

@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(10000)
}

@Benchmark
def groupMapReduce(bh: Blackhole): Unit =
xs.groupMapReduce(_ % size)(_ * 2)(_ + _).foreach(bh.consume)

}

@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 8)
@Measurement(iterations = 8)
@State(Scope.Benchmark)
class GroupMapValuesReduceBenchmark {

type CC[A] = immutable.List[A]
val factory = immutable.List

@Param(scala.Array("2", "3", "5", "16", "17", "32", "33", "128", "129"))
var size: Int = _

var xs: CC[Long] = _

def fresh(n: Int) = factory((1 to n).map(_.toLong): _*)

@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(10000)
}

@Benchmark
def groupMapReduce(bh: Blackhole): Unit =
xs.groupBy(_ % size).mapValues(_.map(_ * 2).reduce(_ + _)).foreach(bh.consume)

}


@BenchmarkMode(scala.Array(Mode.AverageTime))
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 8)
@Measurement(iterations = 8)
@State(Scope.Benchmark)
class ScalaGroupMapValuesReduceBenchmark {

type CC[A] = scala.collection.immutable.List[A]
val factory = scala.collection.immutable.List

@Param(scala.Array("2", "3", "5", "16", "17", "32", "33", "128", "129"))
var size: Int = _

var xs: CC[Long] = _

def fresh(n: Int) = factory((1 to n).map(_.toLong): _*)

@Setup(Level.Trial)
def initTrial(): Unit = {
xs = fresh(10000)
}

@Benchmark
def groupMapReduce(bh: Blackhole): Unit =
xs.groupBy(_ % size).mapValues(_.map(_ * 2).reduce(_ + _)).foreach(bh.consume)

}

0 comments on commit d8bf672

Please sign in to comment.