1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
/**
*/
public class Test2D {
enum Test {
SHARED, ALLOCATE, PRIMITIVE;
}
public Test2D() {
}
public static void main(String... args) {
new Test2D().outputDrawing();
}
public void outputDrawing() {
// the drawing code is probably very low impact, but it's still a
// good idea to wrap it in a Thread to free up processor time.
(new Thread() {
public void run() {
for (Test test : Test.values()) {
BufferedImage bim = null;
int times = 100;
float meanTime = 0;
for (int i = 0; i < times; i++) {
long time1 = System.currentTimeMillis();
bim = bakePieGraph(bim, test);
long time2 = System.currentTimeMillis();
meanTime += time2 - time1;
if (i % (times / 10) == 0) {
System.out.println(i);
}
}
System.out.println();
System.out.println("Test: " + test + "\tRuns: " + times + "\tMean Time: " + (meanTime / times) + "ms");
}
}
}).start();
}
public BufferedImage bakePieGraph(BufferedImage myImage, Test test) {
if (myImage == null) {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
myImage = gc.createCompatibleImage(320, 200);
}
Graphics2D g = myImage.createGraphics();
try {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 320, 200);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
g.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
g.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
g.setColor(Color.BLACK);
Line2D.Float line = (test == Test.SHARED) ? new Line2D.Float() : null;
for (int i = 0; i < 450000; i++) {
switch (test) {
case SHARED:
line.setLine(10, 10, 20, 25);
g.draw(line);
break;
case ALLOCATE:
line = new Line2D.Float(10, 10, 20, 25);
g.draw(line);
break;
case PRIMITIVE:
g.drawLine(10, 10, 20, 25);
}
}
}
finally {
g.dispose();
}
return myImage;
}
} |
Partager