История изменений
Исправление kamre, (текущая версия) :
Переделал на такой вариант:
public class CalcVectors {
private static class Vector2D {
public double x, y;
private Vector2D(double x, double y) {
this.x = x;
this.y = y;
}
public Vector2D add(Vector2D v) {
x += v.x;
y += v.y;
return this;
}
public Vector2D mult(Transform2D t) {
double x1 = x * t.cos - y * t.sin + t.tx;
double y1 = y * t.sin + y * t.cos + t.ty;
x = x1;
y = y1;
return this;
}
@Override
public String toString() {
return "Vector2D(" + "x=" + x + ", y=" + y + ')';
}
}
private static class Transform2D {
public double cos, sin, tx, ty;
private Transform2D(double angle, double tx, double ty) {
this.cos = Math.cos(angle);
this.sin = Math.sin(angle);
this.tx = tx;
this.ty = ty;
}
}
private static class VectorOfVector2D {
private double[] buffer;
private int size;
private VectorOfVector2D(int size) {
this.size = size;
buffer = new double[2 * size];
}
public Vector2D get(int idx) {
return new Vector2D(buffer[idx * 2], buffer[idx * 2 + 1]);
}
public void set(int idx, Vector2D v) {
buffer[2 * idx] = v.x;
buffer[2 * idx + 1] = v.y;
}
public int length() {
return size;
}
}
private static Vector2D calc(VectorOfVector2D v, Transform2D t) {
Vector2D res = new Vector2D(0, 0);
for (int i = 0; i < v.length(); ++i) {
res.add(v.get(i).mult(t));
}
return res;
}
private static void run(String str, VectorOfVector2D vects, Transform2D transf) {
System.out.println("Start of " + str);
Vector2D vector = null;
int repeat = 1000;
long started = System.nanoTime();
for (int i = 0; i < repeat; ++i) {
vector = calc(vects, transf);
}
double totalTime = (System.nanoTime() - started) * 1e-9;
System.out.println("Res = " + vector);
System.out.println("Total time = " + totalTime + " (sec)");
System.out.println("Average time = " + totalTime / repeat + " (sec)");
System.out.println("End of " + str);
}
public static void main(String[] args) {
final int size = 1000000;
VectorOfVector2D vects = new VectorOfVector2D(size);
for (int i = 0; i < size; ++i) {
double d = (1.0 * i) / size;
vects.set(i, new Vector2D(d, d));
}
Transform2D transf = new Transform2D(1.0, -2.5, 5.0);
run("Warm Up", vects, transf);
System.out.println();
run("Actual", vects, transf);
}
}
$java -server -XX:+PrintCompilation CalcVectors
198 1 java.lang.Object::<init> (1 bytes)
200 2 CalcVectors$Vector2D::<init> (7 bytes)
209 3 CalcVectors$Vector2D::<init> (15 bytes)
211 4 CalcVectors$VectorOfVector2D::set (27 bytes)
213 1 % CalcVectors::main @ 16 (92 bytes)
240 1 % CalcVectors::main @ -2 (92 bytes) made not entrant
Start of Warm Up
253 5 CalcVectors$VectorOfVector2D::length (5 bytes)
256 6 CalcVectors$VectorOfVector2D::get (27 bytes)
257 7 CalcVectors$Vector2D::mult (64 bytes)
262 8 CalcVectors$Vector2D::add (28 bytes)
263 2 % CalcVectors::calc @ 13 (43 bytes)
290 9 CalcVectors::calc (43 bytes)
Res = Vector2D(x=-2650584.18888554, y=5690885.954451363)
Total time = 7.934098328 (sec)
Average time = 0.007934098328000001 (sec)
End of Warm Up
Start of Actual
Res = Vector2D(x=-2650584.18888554, y=5690885.954451363)
Total time = 7.874191606 (sec)
Average time = 0.007874191606 (sec)
End of Actual
Исходная версия kamre, :
Переделал на такой вариант:
public class CalcVectors {
private static class Vector2D {
public double x, y;
private Vector2D(double x, double y) {
this.x = x;
this.y = y;
}
public Vector2D add(Vector2D v) {
x += v.x;
y += v.y;
return this;
}
public Vector2D mult(Transform2D t) {
double x1 = x * t.cos - y * t.sin + t.tx;
double y1 = y * t.sin + y * t.cos + t.ty;
x = x1;
y = y1;
return this;
}
@Override
public String toString() {
return "Vector2D(" + "x=" + x + ", y=" + y + ')';
}
}
private static class Transform2D {
public double cos, sin, tx, ty;
private Transform2D(double angle, double tx, double ty) {
this.cos = Math.cos(angle);
this.sin = Math.sin(angle);
this.tx = tx;
this.ty = ty;
}
}
private static class VectorOfVector2D {
private double[] buffer;
private int size;
private VectorOfVector2D(int size) {
this.size = size;
buffer = new double[2 * size];
}
public Vector2D get(int idx) {
return new Vector2D(buffer[idx * 2], buffer[idx * 2 + 1]);
}
public void set(int idx, Vector2D v) {
buffer[2 * idx] = v.x;
buffer[2 * idx + 1] = v.y;
}
public int length() {
return size;
}
}
private static Vector2D calc(VectorOfVector2D v, Transform2D t) {
Vector2D res = new Vector2D(0, 0);
for (int i = 0; i < v.length(); ++i) {
res.add(v.get(i).mult(t));
}
return res;
}
private static void run(String str, VectorOfVector2D vects, Transform2D transf) {
System.out.println("Start of " + str);
Vector2D vector = null;
int repeat = 1000;
long started = System.nanoTime();
for (int i = 0; i < repeat; ++i) {
vector = calc(vects, transf);
}
double totalTime = (System.nanoTime() - started) * 1e-9;
System.out.println("Res = " + vector);
System.out.println("Total time = " + totalTime + " (sec)");
System.out.println("Average time = " + totalTime / repeat + " (sec)");
System.out.println("End of " + str);
}
public static void main(String[] args) {
final int size = 1000000;
VectorOfVector2D vects = new VectorOfVector2D(size);
for (int i = 0; i < size; ++i) {
double d = (1.0 * i) / size;
vects.set(i, new Vector2D(d, d));
}
Transform2D transf = new Transform2D(1.0, -2.5, 5.0);
run("Warm Up", vects, transf);
System.out.println();
run("Actual", vects, transf);
}
}
$java -server -XX:+PrintCompilation CalcVectors
198 1 java.lang.Object::<init> (1 bytes)
200 2 CalcVectors$Vector2D::<init> (7 bytes)
209 3 CalcVectors$Vector2D::<init> (15 bytes)
211 4 CalcVectors$VectorOfVector2D::set (27 bytes)
213 1 % CalcVectors::main @ 16 (92 bytes)
240 1 % CalcVectors::main @ -2 (92 bytes) made not entrant
Start of Warm Up
253 5 CalcVectors$VectorOfVector2D::length (5 bytes)
256 6 CalcVectors$VectorOfVector2D::get (27 bytes)
257 7 CalcVectors$Vector2D::mult (64 bytes)
262 8 CalcVectors$Vector2D::add (28 bytes)
263 2 % CalcVectors::calc @ 13 (43 bytes)
290 9 CalcVectors::calc (43 bytes)
Res = Vector2D(x=-2650584.18888554, y=5690885.954451363)
Total time = 7.934098328 (sec)
Average time = 0.007934098328000001 (sec)
End of Warm Up
Start of Actual
Res = Vector2D(x=-2650584.18888554, y=5690885.954451363)
Total time = 7.874191606 (sec)
Average time = 0.007874191606 (sec)
End of Actual