Paste: Scala Version

Author: Justin Sher
Mode: java
Date: Sat, 29 Aug 2009 07:45:51
Plain Text |
// Runs slightly faster than java

package bench.factor

 object Silly { 

   case class Point(x: Float,y:Float, z:Float);

   object Point {
    
    def pointNorm(p:Point) : Double = {
    	Math.sqrt(p.x * p.x + p.y * p.y + p.z * p.z)
    }
    
    def normalizePoint(p: Point) : Point = { 
      val norm= pointNorm(p).asInstanceOf[Float]
      Point(p.x/norm,p.y/norm,p.z/norm);
    }
    
    def maxPoint(q:Point, p: Point) : Point= {
    	 Point(Math.max(q.x,p.x),Math.max(q.y,p.y),Math.max(q.z,p.z))
    }
    
    def  toString(p: Point) = { 
       p.x+", "+p.y+", "+p.z;
    }
     
    def create(n : Int)  = { 
    		 val s=Math.sin(n);
    		 Point(Math.sin(n).asInstanceOf[Float],Math.cos(n).asInstanceOf[Float]*3.0f,(s * s).asInstanceOf[Float] / 2)         
      }
    }
  
  
  def benchmark(len :Int)  = { 
    val max=maxPoint(new Point(0,0,0),(for (i <- 0 to len-1) yield Point.create(i)) map ( a => { Point.normalizePoint(a) } ))
    System.out.println(max)
  }

  def maxPoint(max: Point ,points: Seq[Point]) : Point ={ 
	  if (points.isEmpty) { return max; }
	  maxPoint(Point.maxPoint(max,points.first),points.drop(1));
  }
  
  def main(args: Array[String]): Unit =
  {
		for(i <- 0 to 8 )
		{
			System.out.println("Run #" + i)
			val start = System.currentTimeMillis()
			benchmark(5000000)
			val end = System.currentTimeMillis()
			System.out.println("Time: " + (end - start));
		}
  }
}

New Annotation

Summary:
Author:
Mode:
Body: