Skip to content

Instantly share code, notes, and snippets.

@sagartewari01 sagartewari01/t.java
Last active Mar 8, 2019

Embed
What would you like to do?
Comparison of reference types vs value types in java
import java.util.*;
class Point {
public final double x;
public final double y;
public Point(double a, double b) {
x = a;
y = b;
}
public double len() {
return Math.sqrt(x*x + y*y);
}
public Point minus(Point o) {
return new Point(x-o.x, y-o.y);
}
}
public class t {
static int size = 1000;
static int iters = 10;
static Point[][] buff = new Point[8][];
static Point[][][] diffs = new Point[8][][];
public static void test (Point[] points, Point[][] diff) {
Random rnd = new Random();
for (int i=0; i<size; i++) {
points[i] = new Point(rnd.nextDouble(), rnd.nextDouble());
}
double sum = 0;
for (int i=0; i<size; i++) {
for (int j=0; j<size; j++) {
diff[i][j] = points[i].minus(points[j]);
}
}
}
static class TestRunner implements Runnable {
final int idx;
TestRunner(int idx) {
this.idx = idx;
}
public void run() {
Point[] arr = new Point[size];
buff[idx] = arr;
Point[][] diff = new Point[size][size];
diffs[idx] = diff;
test(arr, diff);
}
}
public static void main (String[] args) throws InterruptedException {
for (int i=0; i<iters; i++) {
System.out.println(i);
Thread[] threads = new Thread[8];
for (int j=0; j<8; j++) {
threads[j] = new Thread(new TestRunner(j));
threads[j].start();
}
for (int j=0; j<8; j++) {
threads[j].join();
}
useBuff();
System.gc();
}
}
private static void useBuff() {
double sum = 0;
for (Point[] arr : buff) {
for (Point p : arr) {
sum += p.len();
}
}
for (Point[][] diff : diffs) {
for (Point[] diff_arr : diff) {
for (Point p : diff_arr) {
sum += p.len();
}
}
}
System.out.println(sum);
}
}
`
import java.util.*;
class PointStatic {
static class Buffer {
double x;
double y;
}
static public double len(double x, double y) {
return Math.sqrt(x*x + y*y);
}
static public void minus(double x, double y, double o_x, double o_y, Buffer b) {
b.x = x - o_x;
b.y = y - o_y;
}
}
public class u {
static int size = 1000;
static int iters = 10;
static double[][] buff_x = new double[8][];
static double[][] buff_y = new double[8][];
static double[][][] diffs_x = new double[8][][];
static double[][][] diffs_y = new double[8][][];
public static void test (double[] points_x, double[] points_y, double[][] diff_x, double[][] diff_y) {
Random rnd = new Random();
for (int i=0; i<size; i++) {
points_x[i] = rnd.nextDouble();
points_y[i] = rnd.nextDouble();
}
double sum = 0;
PointStatic.Buffer b = new PointStatic.Buffer();
for (int i=0; i<size; i++) {
for (int j=0; j<size; j++) {
PointStatic.minus(points_x[i], points_y[i], points_x[j], points_y[j], b);
diff_x[i][j] = b.x;
diff_y[i][j] = b.y;
}
}
System.gc();
}
static class TestRunner implements Runnable {
final int idx;
TestRunner(int idx) {
this.idx = idx;
}
public void run() {
double[] arr_x = new double[size];
double[] arr_y = new double[size];
buff_x[idx] = arr_x;
buff_y[idx] = arr_y;
double[][] diff_x = new double[size][size];
double[][] diff_y = new double[size][size];
diffs_x[idx] = diff_x;
diffs_y[idx] = diff_y;
test(arr_x, arr_y, diff_x, diff_y);
}
}
public static void main (String[] args) throws InterruptedException {
for (int i=0; i<iters; i++) {
System.out.println(i);
Thread[] threads = new Thread[8];
for (int j=0; j<8; j++) {
threads[j] = new Thread(new TestRunner(j));
threads[j].start();
}
for (int j=0; j<8; j++) {
threads[j].join();
}
useBuff();
System.gc();
}
}
private static void useBuff() {
double sum = 0;
for (int k=0; k<buff_x.length; k++) {
for (int i=0; i<size; i++) {
sum += PointStatic.len(buff_x[k][i], buff_y[k][i]);
}
}
for (int k=0; k<diffs_x.length; k++) {
for (int i=0; i<size; i++) {
for (int j=0; j<size; j++) {
sum += PointStatic.len(diffs_x[k][i][j], diffs_y[k][i][j]);
}
}
}
System.out.println(sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.