Advertisement
Guest User

Untitled

a guest
Sep 13th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.44 KB | None | 0 0
  1. package com.olegchir.vectorperf
  2.  
  3. import scala.collection.mutable.{ListBuffer, ArrayBuffer}
  4.  
  5. object Main {
  6.  
  7.   private trait IVector2D {
  8.     def add(v : IVector2D)
  9.     def mult(t:Transform2D):IVector2D
  10.  
  11.     def xval: Double
  12.     def yval: Double
  13.   }
  14.  
  15.   private class MutableVector2D (var x: Double, var y: Double) extends IVector2D {
  16.     def add(v : IVector2D) = {x = v.xval; y = v.yval}
  17.     def mult(t:Transform2D):MutableVector2D = {x = x * t.cos - y * t.sin + t.tx; y = y * t.sin + y * t.cos + t.ty;this}
  18.  
  19.     def xval = x
  20.     def yval = y
  21.     override def toString = "MutableVector2D(" + "x=" + x + ", y=" + y + ')'
  22.   }
  23.  
  24.   private class ImmutableVector2D (val x: Double, val y: Double) extends IVector2D {
  25.     def add(v : IVector2D) = {new ImmutableVector2D(v.xval,v.yval)}
  26.     def mult(t:Transform2D):IVector2D = {new ImmutableVector2D(x * t.cos - y * t.sin + t.tx, y * t.sin + y * t.cos + t.ty)}
  27.  
  28.     def xval = x
  29.     def yval = y
  30.     override def toString = "ImmutableVector2D(" + "x=" + x + ", y=" + y + ')'
  31.   }
  32.  
  33.   private type Vector2D = ImmutableVector2D
  34.  
  35.   private class Transform2D (val angle: Double, val tx: Double, val ty: Double) {
  36.     val cos: Double = Math.cos(angle)
  37.     val sin: Double = Math.sin(angle)
  38.   }
  39.  
  40.   private def calc(v: List[Vector2D], t: Transform2D): Vector2D = {
  41.     val res = new Vector2D(0, 0)
  42.     v.foreach((vec: Vector2D) => res.add(vec.mult(t)))
  43.     res
  44.   }
  45.  
  46.   implicit def intWithTimes(n: Int) = new {
  47.     def times(f: => Unit) = 1 to n foreach {_ => f}
  48.   }
  49.  
  50.   private def run(str: String, vects: List[Vector2D], transf: Transform2D): Unit = {
  51.     println("Start of " + str)
  52.     var vector: Vector2D = null
  53.     val N = 1000
  54.  
  55.     val started = System.nanoTime()
  56.  
  57.     N times {
  58.       vector = calc(vects, transf)
  59.     }
  60.  
  61.     val totalTime = (System.nanoTime() - started) * 1e-9
  62.  
  63.     println("Res = " + vector)
  64.     println("Total time = " + totalTime + " (sec)")
  65.     println("Average time = " + totalTime / N + " (sec)")
  66.     println("End of " + str)
  67.   }
  68.  
  69.  
  70.   def main(a: Array[String]) = {
  71.     val size = 1000000
  72.     val vectsBuffer: ListBuffer[Vector2D] = new ListBuffer[Vector2D]
  73.  
  74.     for (i <- 0 until size) {
  75.       val d = (1.0 * i) / size
  76.       vectsBuffer += new Vector2D(d, d)
  77.     }
  78.  
  79.     val vects: List[Vector2D] = vectsBuffer.toList
  80.  
  81.     val transf = new Transform2D(1.0, -2.5, 5.0);
  82.     run("Warm Up", vects, transf)
  83.     println
  84.     run("Actual", vects, transf)
  85.   }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement