forked from twitter/summingbird
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request twitter#591 from twitter/sritchie/tuple_performance
optimize Generated{Abstract,Product}Algebra with benchmarking
- Loading branch information
Showing
10 changed files
with
796 additions
and
304 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
algebird-benchmark/src/main/scala/com/twitter/algebird/benchmark/Tuple4Benchmark.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.twitter.algebird | ||
package benchmark | ||
|
||
import scala.util.Random | ||
import org.openjdk.jmh.annotations._ | ||
import org.openjdk.jmh.infra.Blackhole | ||
|
||
import scala.math._ | ||
|
||
object Tuple4Benchmark { | ||
type Long4 = (Long, Long, Long, Long) | ||
@State(Scope.Benchmark) | ||
class Tuple4State { | ||
/** | ||
* This monoid lives in `GeneratedAbstractAlgebra.scala`. | ||
*/ | ||
val tupleMonoid: Monoid[Long4] = implicitly | ||
|
||
/** | ||
* This monoid lives in `GeneratedProductAlgebra.scala`. | ||
*/ | ||
val productMonoid: Monoid[Long4] = | ||
Monoid[Long4, Long, Long, Long, Long](Tuple4.apply, Tuple4.unapply) | ||
|
||
@Param(Array("10000")) | ||
var numElements: Int = 0 | ||
|
||
var inputData: Seq[(Long, Long, Long, Long)] = _ | ||
|
||
private def randL: Long = Random.nextInt(1000).toLong | ||
|
||
@Setup(Level.Trial) | ||
def setup(): Unit = { | ||
inputData = Seq.fill(numElements)((randL, randL, randL, randL)) | ||
} | ||
} | ||
} | ||
|
||
class Tuple4Benchmark { | ||
import Tuple4Benchmark._ | ||
|
||
@Benchmark | ||
def timeTuplePlus(state: Tuple4State, bh: Blackhole) = | ||
bh.consume(state.inputData.reduce(state.tupleMonoid.plus(_, _))) | ||
|
||
@Benchmark | ||
def timeTupleSumOption(state: Tuple4State, bh: Blackhole) = | ||
bh.consume(state.tupleMonoid.sumOption(state.inputData)) | ||
|
||
@Benchmark | ||
def timeProductPlus(state: Tuple4State, bh: Blackhole) = | ||
bh.consume(state.inputData.reduce(state.productMonoid.plus(_, _))) | ||
|
||
@Benchmark | ||
def timeProductSumOption(state: Tuple4State, bh: Blackhole) = | ||
bh.consume(state.productMonoid.sumOption(state.inputData)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.