Last active
August 29, 2015 14:01
-
-
Save BurntPizza/a7ae9e324ad3c6eb1137 to your computer and use it in GitHub Desktop.
This file contains 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
import java.awt.*; | |
import java.awt.image.*; | |
import javax.swing.JFrame; | |
public class Main extends JFrame { | |
public static final int TEST_LINES = 0; | |
public static final int TEST_DATABUFFER_INT = 1; | |
public static final int TEST_DATABUFFER_BYTE = 2; | |
public static final int TEST_SETRGB = 3; | |
public static final int NUM_FRAMES = 1000; | |
public static final byte BYTE_255 = (byte) 0xFF; | |
private BufferedImage buffer; | |
private Graphics2D bGraphics; | |
private int[] pixelsInt; | |
private byte[] pixelsByte; | |
private final int w, h; | |
private int test; | |
public Main(int test) { | |
w = 512; | |
h = 512; | |
this.test = test; | |
if (test == TEST_LINES) { | |
buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); | |
} else if (test == TEST_DATABUFFER_INT) { | |
buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); | |
pixelsInt = ((DataBufferInt) buffer.getRaster().getDataBuffer()).getData(); | |
} else if (test == TEST_DATABUFFER_BYTE) { | |
buffer = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); | |
pixelsByte = ((DataBufferByte) buffer.getRaster().getDataBuffer()).getData(); | |
} else if (test == TEST_SETRGB) { | |
buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); | |
} | |
bGraphics = buffer.createGraphics(); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
setSize(w, h); | |
} | |
public static void main(String[] args) { | |
System.out.println("Rendering Test: " + NUM_FRAMES + " frames..."); | |
System.out.printf("Time for drawLine(): %.2f ms/frame\n", new Main(TEST_LINES).test() / NUM_FRAMES * 1000); | |
System.out.printf("Time for setRGB(): %.2f ms/frame\n", new Main(TEST_SETRGB).test() / NUM_FRAMES * 1000); | |
System.out.printf("Time for int[]: %.2f ms/frame\n", new Main(TEST_DATABUFFER_INT).test() / NUM_FRAMES * 1000); | |
System.out.printf("Time for byte[]: %.2f ms/frame\n", new Main(TEST_DATABUFFER_BYTE).test() / NUM_FRAMES * 1000); | |
} | |
private double test() { | |
setVisible(true); | |
Graphics g = getGraphics(); //to draw buffer to the JFrame | |
long start = System.nanoTime(); | |
for (int i = 0; i < NUM_FRAMES; i++) { | |
renderBuffer(); | |
paint(g); | |
} | |
long time = System.nanoTime() - start; | |
g.dispose(); | |
dispose(); | |
return time / Math.pow(10, 9); //return seconds | |
} | |
private static int seed = 34435636; | |
private static final int rand255() { | |
seed ^= seed << 11; | |
seed ^= seed >>> 25; | |
seed ^= seed << 4; | |
int out = seed % 255; | |
return out < 0 ? -out : out; | |
} | |
private void renderBuffer() { | |
switch (test) { | |
case TEST_LINES: | |
for (int y = 0; y < h; y++) { | |
for (int x = 0; x < w; x++) { | |
bGraphics.setColor(rand255() > 127 ? Color.GREEN : Color.BLUE); //imagine if we had to create a new Color object for each pixel o_O | |
bGraphics.drawLine(x, y, x, y); | |
} | |
} | |
break; | |
case TEST_DATABUFFER_INT: | |
for (int i = 0; i < w * h; i++) { | |
final int c = rand255(); | |
pixelsInt[i] = packInt(0, c, 255 - c); | |
} | |
break; | |
case TEST_DATABUFFER_BYTE: | |
for (int i = 0; i < w * h * 3;) { | |
final byte red = 0; | |
final byte green = (byte) rand255(); | |
final byte blue = (byte) (255 - green); | |
pixelsByte[i++] = blue; | |
pixelsByte[i++] = green; | |
pixelsByte[i++] = red; | |
} | |
break; | |
case TEST_SETRGB: | |
for (int y = 0; y < h; y++) { | |
for (int x = 0; x < w; x++) { | |
final int c = rand255(); | |
buffer.setRGB(x, y, packInt(0, c, 255 - c)); | |
} | |
} | |
break; | |
} | |
} | |
@Override | |
public void paint(Graphics g) { | |
g.drawImage(buffer, 0, 0, null); | |
} | |
@Override | |
public void dispose() { | |
if (bGraphics != null) | |
bGraphics.dispose(); | |
super.dispose(); | |
} | |
private static final int packInt(int r, int g, int b) { | |
return 255 << 24 | r << 16 | g << 8 | b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment