Skip to content

Instantly share code, notes, and snippets.

@rudogma
Last active June 26, 2017 19:01
Show Gist options
  • Save rudogma/d9d7e71d81d8a80a0f5b678e92ec3ade to your computer and use it in GitHub Desktop.
Save rudogma/d9d7e71d81d8a80a0f5b678e92ec3ade to your computer and use it in GitHub Desktop.
Scala native tagged types + superquants 40x slower then jvm ((
clone https://github.com/milessabin/shapeless.git
cd shapeless
sbt
-- in sbt:
++2.11.11
coreNative/publishM2
exit

clone https://github.com/Rudogma/scala-superquants.git
cd scala-superquants
edit project/Build.sbt
find and change version from "0.9" -> "0.9-SNAPSHOT"
sbt
-- in sbt:
++2.11.11
superquantsNative/publishM2
exit

mkdir scalanative-superquants
cd scalanative-superquants
sbt new scala-native/scala-native.g8

edit build.sbt:

scalaVersion := "2.11.11"

enablePlugins(ScalaNativePlugin)

resolvers ++= Seq(
  Resolver.sonatypeRepo("releases"),
  Resolver.sonatypeRepo("snapshots")
)

libraryDependencies ++= Seq(
  "com.chuusai" %%% "shapeless" % "2.3.3-SNAPSHOT",
  "org.rudogma" %%% "supertagged" % "1.2",
  "org.rudogma" %%% "superquants" % "0.9-SNAPSHOT"
)



nativeMode := "release"
nativeGC := "none"
sbt
-- in sbt:
run
package tests
import shapeless.{HNil, Nat, ::}
import superquants._
import doubleprecision._
import doubleprecision.time._
import doubleprecision.length._
object Hello extends App {
println("Hello, World!")
var limit = 1000 * 1000 * 30 //0
Thread.sleep( (1.seconds in Millis).toLong)
println(s"Starting: $limit")
type Acceleration = Complex[Double, Single[Meters] :: Negative.Squared[Seconds] :: HNil]
@inline def calculate_distance_typed(time: Seconds, acceleration: Acceleration): Meters = {
val squaredTime = time ** time
val distance = acceleration ** squaredTime divide 2d
distance
}
{
var total = 0d
bench("Squants", count = 5) { i =>
var i = 0
while( i < limit){
val acceleration:Acceleration = (i.toDouble % 30000).as[Acceleration]
val time = (i.toDouble % 1000).as[Seconds]
total += calculate_distance_typed(time, acceleration)
i += 1
}
}
println(s"total: ${total}")
}
@inline def calculate_distance_primitive(timeInSeconds: Double, acceleration: Double): Double = {
val squaredTimeInSeconds = timeInSeconds * timeInSeconds
val distance = acceleration * squaredTimeInSeconds / 2
distance
}
{
var total = 0d
bench("Raw Double", count = 5) { i =>
var i = 0
while( i < limit){
val acceleration = i.toDouble % 30000
val time = i.toDouble % 1000
total += calculate_distance_primitive( time, acceleration.toDouble)
i += 1
}
}
println(s"total: ${total}")
}
}
import superquants.longprecision.time._
import supertagged.TaggedType
package object tests {
/** MICRO BENCH **/
object BenchIndex extends TaggedType[Int]
type BenchIndex = BenchIndex.Type
def bench(title:String = "default", count:Int=1, sleep:Milliseconds = 100.millis)(f: BenchIndex => Unit): Long ={
var measures = List.empty[Long]
var i = 0
while( i < count){
val started_at = System.currentTimeMillis()
f(BenchIndex @@ i)
val duration = System.currentTimeMillis() - started_at
println(s"[run#${i}] ended in ${duration}ms")
measures = measures :+ duration
i = i + 1
if(sleep > 0){
Thread.sleep(sleep)
}
}
// for( (measure,index) <- measures.zipWithIndex){
// println(s"[run#${index}] ended in ${measure}ms")
// }
val total_duration = measures.sum
println(s"[bench][${title}][runs=$count] ended in ${total_duration}ms")
total_duration
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment