Skip to content

Commit

Permalink
add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
sritchie committed Jan 13, 2022
1 parent e4403cb commit dfeafa1
Showing 1 changed file with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.twitter.algebird
package benchmark

import scala.util.Random

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

object MomentsBenchmark {
@State(Scope.Benchmark)
class MomentsState {
@Param(Array("10000"))
var numElements: Int = 0

var inputData: Seq[Double] = _
var inputMoments: Seq[Moments] = _

@Setup(Level.Trial)
def setup(): Unit = {
inputData = Seq.fill(numElements)(Random.nextInt(1000).toLong)
inputMoments = inputData.map(Moments(_))
}
}
}

class MomentsBenchmark {
import MomentsBenchmark._

@Benchmark
def timePlusDoubles(state: MomentsState, bh: Blackhole): Unit =
bh.consume(
state.inputData.foldLeft(Moments.momentsMonoid.zero)(_ + _)
)

@Benchmark
def timePlusMoments(state: MomentsState, bh: Blackhole): Unit =
bh.consume(state.inputMoments.reduce(_ + _))

@Benchmark
def timeSumOption(state: MomentsState, bh: Blackhole): Unit =
bh.consume(Moments.momentsMonoid.sumOption(state.inputMoments))

@Benchmark
def timeFold(state: MomentsState, bh: Blackhole): Unit =
bh.consume(Moments.fold.overTraversable(state.inputData))

@Benchmark
def timeAggregate(state: MomentsState, bh: Blackhole): Unit =
bh.consume(Moments.aggregator(state.inputData))
}

0 comments on commit dfeafa1

Please sign in to comment.