-
Notifications
You must be signed in to change notification settings - Fork 346
Coverage and documentation for AveragedValue #589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
36 changes: 36 additions & 0 deletions
36
...bird-benchmark/src/main/scala/com/twitter/algebird/benchmark/AveragedValueBenchmark.scala
This file contains hidden or 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,36 @@ | ||
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 AveragedValueBenchmark { | ||
@State(Scope.Benchmark) | ||
class AVState { | ||
@Param(Array("10000")) | ||
var numElements: Int = 0 | ||
|
||
var inputData: Seq[AveragedValue] = _ | ||
|
||
@Setup(Level.Trial) | ||
def setup(): Unit = { | ||
inputData = Seq.fill(numElements)(AveragedValue(Random.nextInt(1000).toLong)) | ||
} | ||
} | ||
} | ||
|
||
class AveragedValueBenchmark { | ||
import AveragedValueBenchmark._ | ||
import AveragedGroup.{ plus, sumOption } | ||
|
||
@Benchmark | ||
def timePlus(state: AVState, bh: Blackhole) = | ||
bh.consume(state.inputData.reduce(plus(_, _))) | ||
|
||
@Benchmark | ||
def timeSumOption(state: AVState, bh: Blackhole) = | ||
bh.consume(sumOption(state.inputData)) | ||
} |
6 changes: 3 additions & 3 deletions
6
algebird-benchmark/src/main/scala/com/twitter/algebird/benchmark/HLLBenchmark.scala
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -16,32 +16,63 @@ limitations under the License. | |
package com.twitter.algebird | ||
|
||
/** | ||
* First tracks the "most recent" item by the order in which items | ||
* are seen. | ||
* Tracks the "least recent", or earliest, wrapped instance of `T` by | ||
* the order in which items are seen. | ||
* | ||
* @param get wrapped instance of `T` | ||
*/ | ||
case class First[@specialized(Int, Long, Float, Double) +T](get: T) { | ||
/** | ||
* Returns this instance, always. | ||
* | ||
* @param r ignored instance of `First[U]` | ||
*/ | ||
def +[U >: T](r: First[U]): First[U] = this | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could actually make this return |
||
} | ||
|
||
/** | ||
* Provides a set of operations and typeclass instances needed to use | ||
* [[First]] instances. | ||
*/ | ||
object First extends FirstInstances { | ||
/** | ||
* Returns an [[Aggregator]] that selects the first instance of `T` | ||
* in the aggregated stream. | ||
*/ | ||
def aggregator[T]: FirstAggregator[T] = FirstAggregator() | ||
} | ||
|
||
private[algebird] sealed abstract class FirstInstances { | ||
def firstSemigroup[T] = new Semigroup[T] { | ||
def plus(l: T, r: T): T = l | ||
/** | ||
* Returns a [[Semigroup]] instance with a `plus` implementation | ||
* that always returns the first (ie, the left) `T` argument. | ||
* | ||
* This semigroup's `sumOption` is efficient; it only selects the | ||
* head of the `TraversableOnce` instance, leaving the rest | ||
* untouched. | ||
*/ | ||
def firstSemigroup[T]: Semigroup[T] = | ||
new Semigroup[T] { | ||
def plus(l: T, r: T): T = l | ||
|
||
override def sumOption(iter: TraversableOnce[T]): Option[T] = | ||
if (iter.isEmpty) None else Some(iter.toIterator.next) | ||
} | ||
override def sumOption(iter: TraversableOnce[T]): Option[T] = | ||
if (iter.isEmpty) None else Some(iter.toIterator.next) | ||
} | ||
|
||
/** | ||
* Returns a [[Semigroup]] instance for [[First]][T]. The `plus` | ||
* implementation always returns the first (ie, the left) `First[T]` | ||
* argument. | ||
*/ | ||
implicit def semigroup[T]: Semigroup[First[T]] = firstSemigroup[First[T]] | ||
} | ||
|
||
/** | ||
* [[Aggregator]] that selects the first instance of `T` in the | ||
* aggregated stream. | ||
*/ | ||
case class FirstAggregator[T]() extends Aggregator[T, T, T] { | ||
def prepare(v: T) = v | ||
|
||
val semigroup: Semigroup[T] = First.firstSemigroup[T] | ||
|
||
def present(v: T) = v | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
while we are at it, should we add:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!