# Paste: struct array benchmark -- java version

Author: slava java Fri, 28 Aug 2009 09:19:32
Plain Text |
```public class silly
{
static class Point
{
float x, y, z;

Point() { }

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);
}

void normalizePoint()
{
double norm = pointNorm();
x /= norm;
y /= norm;
z /= norm;
}

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);
System.out.println(maxPoints(points));
}

public static void main(String[] args)
{
for(int i = 0; i < 8; i++)
{
System.out.println("Run #" + i);
long start = System.currentTimeMillis();
benchmark(5000000);
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - start));
}
}
}```

## Annotation: Common Lisp Struct Arrays

Author: George Rogers lisp Sat, 9 Jul 2011 02:04:15
Plain Text |
```(defun square (x)
(* x x))
(defstruct point
(x 0.0 :type float)
(y 0.0 :type float)
(z 0.0 :type float))
(defun init-point (n point)
(let* ((x (coerce n 'float))
(y (sin x)))
(setf (point-x point) y)
(setf (point-y point) (* 3 (cos x)))
(setf (point-z point) (/ (* y y) 2))))
(defun make-points (len)
(let ((points (make-array len :element-type 'point)))
(dotimes (i len)
(let ((point (make-point)))
(init-point i point)
(setf (elt points i) point)))
points))
(defun point-norm (point)
(sqrt (+ (square (point-x point)) (square (point-y point))
(square (point-z point)))))
(defun normalize-point (point)
(let ((x (point-norm point)))
(setf (point-x point) (/ (point-x point) x))
(setf (point-y point) (/ (point-y point) x))
(setf (point-z point) (/ (point-z point) x))))
(defun max-point (point-a point-b)
(setf (point-x point-a) (max (point-x point-a) (point-x point-b)))
(setf (point-y point-a) (max (point-y point-a) (point-y point-b)))
(setf (point-z point-a) (max (point-z point-a) (point-z point-b)))
point-a)
(defun print-point (point)
(format t "~a, ~a, ~a~%" (point-x point) (point-y point) (point-z point)))
(defun bench ()
(let ((points (make-points 500000)))
(map nil #'normalize-point points)
(print-point
(reduce #'max-point points
:initial-value (make-point :x 0.0 :y 0.0 :z 0.0)))))
(defun stime (x)
(let* ((a (get-internal-real-time))
(b (funcall x))
(c (get-internal-real-time)))
(format t "Time: ~a~%" (- c a))
b))
(dotimes (i 8)
(format t "Run #~a~%" i)
(stime #'bench))```