Skip to content

Commit

Permalink
MacroExtensions: reorganized Complex example + added non-inlined memb…
Browse files Browse the repository at this point in the history
…ers to benchmark
  • Loading branch information
ochafik committed Feb 25, 2013
1 parent 2d98cf3 commit ece8b00
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
@@ -1,7 +1,10 @@
/*
sbt clean && sbt "project Usage" "run 0" "run 1" "run 2" "run 3"
sbt clean && sbt "run 0" "run 1" "run 2" "run 3" "run 4"
; clean ; run 0 ; run 1 ; run 2 ; run 3 ; run 4
*/
import scalaxy.loops._
import scala.language.postfixOps

// TODO hygienize self and params (by value, not by name as currently)
object Run extends App {
Expand Down Expand Up @@ -29,12 +32,18 @@ object Run extends App {
r = r * b + a
r
}
def Members(a: Complex, b: Complex) = {
def NormalMembers(a: Complex, b: Complex) = {
var r = a
for (i <- 0 until n optimized)
r = (r ** b) ++ a
r
}
def InlineMembers(a: Complex, b: Complex) = {
var r = a
for (i <- 0 until n optimized)
r = (r *** b) +++ a
r
}

val x = Complex(1.02, 0.014)
val y = Complex(0.8, 0.002)
Expand All @@ -44,6 +53,7 @@ object Run extends App {
case 0 => tst(n, "Naive Extensions") { NaiveExtensions(x, y) }
case 1 => tst(n, "Inline Extensions") { InlineExtensions(x, y) }
case 2 => tst(n, "Macro Extensions") { MacroExtensions(x, y) }
case 3 => tst(n, "Inline Members") { Members(x, y) }
case 3 => tst(n, "Normal Members") { NormalMembers(x, y) }
case 4 => tst(n, "Inline Members") { InlineMembers(x, y) }
}
}
20 changes: 14 additions & 6 deletions MacroExtensions/examples/Complex/Library/Complex.scala
@@ -1,13 +1,21 @@
final case class Complex(real: Double, imag: Double) {
@inline
def **(y: Complex) =
Complex(
this.real * y.real - this.imag * y.imag,
this.real * y.imag + this.imag * y.real)

@inline
def ++(y: Complex) =
Complex(this.real + y.real, this.imag + y.imag)

@inline
def ***(y: Complex) =
Complex(
this.real * y.real - this.imag * y.imag,
this.real * y.imag + this.imag * y.real)

@inline
def +++(y: Complex) =
Complex(this.real + y.real, this.imag + y.imag)
}

object NaiveImplicits
Expand Down Expand Up @@ -49,16 +57,16 @@ object MacroImplicits
{
@scalaxy.extension[Complex]
def module: Double =
self.real * self.real + self.imag * self.imag
this.real * this.real + this.imag * this.imag

@scalaxy.extension[Complex]
def *(y: Complex): Complex =
Complex(
self.real * y.real - self.imag * y.imag,
self.real * y.imag + self.imag * y.real)
this.real * y.real - this.imag * y.imag,
this.real * y.imag + this.imag * y.real)

@scalaxy.extension[Complex]
def +(y: Complex): Complex =
Complex(self.real + y.real, self.imag + y.imag)
Complex(this.real + y.real, this.imag + y.imag)
}

13 changes: 0 additions & 13 deletions MacroExtensions/examples/Complex/Usage/build.sbt

This file was deleted.

14 changes: 13 additions & 1 deletion MacroExtensions/examples/Complex/build.sbt
@@ -1 +1,13 @@
scalaVersion in ThisBuild := "2.10.0"
// Only works with 2.10.0+
scalaVersion := "2.10.0"

// Uncomment this to see what's happening:
//scalacOptions ++= Seq("-Xprint:parser", "-Xprint:refchecks")

scalacOptions ++= Seq("-optimise", "-Yinline", "-Yclosure-elim", "-feature", "-deprecation")

libraryDependencies += "com.nativelibs4java" %% "scalaxy-loops" % "0.3-SNAPSHOT" % "provided"

resolvers += Resolver.sonatypeRepo("snapshots")

fork := true
4 changes: 1 addition & 3 deletions MacroExtensions/examples/Complex/project/Build.scala
Expand Up @@ -2,9 +2,7 @@ import sbt._
import Keys._

object Build extends Build {
lazy val root = Project(id = "Root", base = file(".")).aggregate(library, usage)
lazy val root = Project(id = "Test", base = file(".")).aggregate(library).dependsOn(library)

lazy val library = Project(id = "Library", base = file("Library"))

lazy val usage = Project(id = "Usage", base = file("Usage")).dependsOn(library)
}

0 comments on commit ece8b00

Please sign in to comment.