Created
August 28, 2019 22:43
-
-
Save JaroslavTulach/3d10da1ad1cc2e6fb7d9bacb78ea77ea to your computer and use it in GitHub Desktop.
Demonstrating the usefulness of PGO (profile guided optimizations).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class Shape { | |
public abstract double area(); | |
public static Shape cicle(double radius) { | |
return new Circle(radius); | |
} | |
public static Shape square(double side) { | |
return new Square(side); | |
} | |
public static Shape rectangle(double a, double b) { | |
return new Rectangle(a, b); | |
} | |
public static Shape triangle(double base, double height) { | |
return new Triagle(base, height); | |
} | |
static class Circle extends Shape { | |
private final double radius; | |
Circle(double radius) { | |
this.radius = radius; | |
} | |
@Override | |
public double area() { | |
return Math.PI * Math.pow(this.radius, 2); | |
} | |
} | |
static class Square extends Shape { | |
private final double side; | |
Square(double side) { | |
this.side = side; | |
} | |
@Override | |
public double area() { | |
return Math.pow(side, 2); | |
} | |
} | |
static class Rectangle extends Shape { | |
private final double a; | |
private final double b; | |
Rectangle(double a, double b) { | |
this.a = a; | |
this.b = b; | |
} | |
@Override | |
public double area() { | |
return a * b; | |
} | |
} | |
static class Triagle extends Shape { | |
private final double base; | |
private final double height; | |
Triagle(double base, double height) { | |
this.base = base; | |
this.height = height; | |
} | |
@Override | |
public double area() { | |
return base * height / 2; | |
} | |
} | |
static double computeArea(Shape[] all) { | |
double sum = 0; | |
for (Shape shape : all) { | |
sum += shape.area(); | |
} | |
return sum; | |
} | |
public static void main(String[] args) throws Exception { | |
int cnt = Integer.parseInt(args[0]); | |
long seed = Long.parseLong(args[1]); | |
int repeat = Integer.parseInt(args[2]); | |
Shape[] samples = generate(3, args, cnt, seed); | |
double expected = computeArea(samples); | |
long prev = System.currentTimeMillis(); | |
for (int i = 0; i < repeat * 1000; i++) { | |
double sum = computeArea(samples); | |
if (sum != expected) { | |
throw new IllegalStateException(); | |
} | |
if (i % 1000 == 0) { | |
prev = System.currentTimeMillis(); | |
} | |
} | |
System.err.println("last round " + (System.currentTimeMillis() - prev) + " ms."); | |
} | |
static Shape[] generate(int offset, String[] types, int count, long seed) { | |
java.util.Random r = new java.util.Random(seed); | |
Shape[] arr = new Shape[count]; | |
for (int i = 0; i < arr.length; i++) { | |
String t = types[offset + i % (types.length - offset)]; | |
Shape s; | |
switch (t) { | |
case "circle": | |
s = Shape.cicle(r.nextDouble()); | |
break; | |
case "rectangle": | |
s = Shape.rectangle(r.nextDouble(), r.nextDouble()); | |
break; | |
case "square": | |
s = Shape.square(r.nextDouble()); | |
break; | |
case "triangle": | |
s = Shape.triangle(r.nextDouble(), r.nextDouble()); | |
break; | |
default: | |
throw new IllegalStateException("" + t); | |
} | |
arr[i] = s; | |
} | |
return arr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment