Paste: silly benchmark in C#
Author: | Z.T. |
Mode: | c# |
Date: | Fri, 28 Aug 2009 16:44:53 |
Plain Text |
using System;
namespace csharp
{
public class silly
{
struct Point
{
float x, y, z;
public Point(int n)
{
this.x = (float)Math.Sin(n);
this.y = (float)Math.Cos(n) * 3;
double s = Math.Sin(n);
this.z = (float)((s * s) / 2);
}
double pointNorm()
{
return Math.Sqrt(x * x + y * y + z * z);
}
public void normalizePoint()
{
double norm = pointNorm();
x = (float)(x / norm);
y = (float)(y / norm);
z = (float)(z / norm);
}
public void maxPoint(Point p)
{
x = Math.Max(x,p.x);
y = Math.Max(y,p.y);
z = Math.Max(z,p.z);
}
public String toString()
{
return x + ", " + y + ", " + z;
}
}
static Point[] makePoints(int len)
{
Point[] points = new Point[len];
for(int i = 0; i < len; i++)
points[i] = new Point(i);
return points;
}
static void normalizePoints(Point[] points)
{
for(int i = 0; i < points.Length; i++)
points[i].normalizePoint();
}
static Point maxPoints(Point[] points)
{
Point max = new Point();
for(int i = 0; i < points.Length; i++)
max.maxPoint(points[i]);
return max;
}
static void benchmark(int len)
{
Point[] points = makePoints(len);
normalizePoints(points);
Console.WriteLine(maxPoints(points));
}
public static void Main(string[] args)
{
for(int i = 0; i < 20; i++)
{
Console.WriteLine("Run #" + i);
long start = System.DateTime.Now.Ticks;
benchmark(5000000);
long end = System.DateTime.Now.Ticks;
Console.WriteLine("Time: " + (end - start) / 10000);
}
}
}
}
New Annotation