Skip to content

Instantly share code, notes, and snippets.

@tixxit
Last active December 18, 2015 00:18
Show Gist options
  • Save tixxit/5695365 to your computer and use it in GitHub Desktop.
Save tixxit/5695365 to your computer and use it in GitHub Desktop.

Late 2011 MacBook Air, Oracle HotSpot 7.

gcdDirect:        29.981   **1.00 x gcdDirect**
gcdSpire:         30.094   **1.00 x gcdDirect**
gcdSpireNonSpec:  36.903   **1.23 x gcdDirect** 
gcdScala:         38.989   **1.30 x gcdDirect**
package benchmark
import org.scalameter.api._
import spire.algebra._
import spire.math._
import spire.syntax._
import spire.std.long._
object GCDPerfTest extends PerformanceTest {
def gcdDirect(x: Long, y: Long): Long =
if (y == 0L) x else gcdDirect(y, x % y)
def gcdScala[A: scala.math.Integral](x: A, y: A): A = {
import scala.math.Integral.Implicits._
if (y == 0) x else gcdScala(y, x % y)
}
def gcdSpireNonSpec[A: Integral](x: A, y: A): A = {
import spire.ops._
if (y == 0) x else gcdSpireNonSpec(y, x % y)
}
def gcdSpire[@specialized(Long) A: Integral](x: A, y: A): A = {
import spire.ops._
if (y == 0) x else gcdSpire(y, x % y)
}
val longs = Gen.single("random")(Array.fill(50000)(scala.util.Random.nextLong))
performance of "gcd" in {
measure method "gcdDirect" in {
using(longs) in { xs =>
cfor(1)(_ < xs.length, _ + 1) { i => gcdDirect(xs(i), xs(i - 1)) }
}
}
measure method "gcdScala" in {
using(longs) in { xs =>
cfor(1)(_ < xs.length, _ + 1) { i => gcdScala(xs(i), xs(i - 1)) }
}
}
measure method "gcdSpireNonSpec" in {
using(longs) in { xs =>
cfor(1)(_ < xs.length, _ + 1) { i => gcdSpireNonSpec(xs(i), xs(i - 1)) }
}
}
measure method "gcdSpire" in {
using(longs) in { xs =>
cfor(1)(_ < xs.length, _ + 1) { i => gcdSpire(xs(i), xs(i - 1)) }
}
}
}
lazy val executor = LocalExecutor(
new Executor.Warmer.Default,
Aggregator.min,
new Measurer.Default)
lazy val reporter = new LoggingReporter
lazy val persistor = Persistor.None
}

Late 2011 MacBook Air, Oracle HotSpot 7.

meanDirect:        10.592   **1.00 x gcdDirect**
meanSpire:         10.638   **1.00 x gcdDirect**
meanSpireNonSpec:  13.434   **1.26 x gcdDirect** 
meanScala:         19.388   **1.83 x gcdDirect**
package benchmark
import org.scalameter.api._
import spire.algebra._
import spire.math._
import spire.syntax._
import spire.std.double._
object MeanPerfTest extends HedralPerformanceTest {
def meanDirect(xs: Array[Double]): Double = {
var mean = 0D
cfor(0)(_ < xs.length, _ + 1) { i =>
mean += (xs(i) - mean) / (i + 1)
}
mean
}
def meanScala[A](xs: Array[A])(implicit num: scala.math.Fractional[A]): A = {
import scala.math.Fractional.Implicits._
var mean = num.zero
cfor(0)(_ < xs.length, _ + 1) { i =>
mean += (xs(i) - mean) / (num.fromInt(i) + num.one)
}
mean
}
def meanSpireNonSpec[A: Fractional](xs: Array[A]): A = {
import spire.ops._
var mean = Fractional[A].zero
cfor(0)(_ < xs.length, _ + 1) { i =>
mean += (xs(i) - mean) / (i + 1)
}
mean
}
def meanSpire[@specialized(Double) A: Fractional](xs: Array[A]): A = {
import spire.ops._
var mean = Fractional[A].zero
cfor(0)(_ < xs.length, _ + 1) { i =>
mean += (xs(i) - mean) / (i + 1)
}
mean
}
val doubles = Gen.single("random")(Array.fill(1000000)(scala.util.Random.nextGaussian))
performance of "mean" in {
measure method "meanDirect" in {
using(doubles) in { xs =>
meanDirect(xs)
}
}
measure method "meanScala" in {
using(doubles) in { xs =>
meanScala(xs)
}
}
measure method "meanSpireNonSpec" in {
using(doubles) in { xs =>
meanSpireNonSpec(xs)
}
}
measure method "meanSpire" in {
using(doubles) in { xs =>
meanSpire(xs)
}
}
}
lazy val executor = LocalExecutor(
new Executor.Warmer.Default,
Aggregator.min,
new Measurer.Default)
lazy val reporter = new LoggingReporter
lazy val persistor = Persistor.None
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment