-
-
Save BurntPizza/7a5b93d3eaa9f910c8c6 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 java.util.Arrays; | |
import javax.swing.JFrame; | |
public class Main extends JFrame { | |
public static final int TEST_GRAPHICS = 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_GRAPHICS) { | |
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 fillRect(): %.2f ms/frame\n", new Main(TEST_GRAPHICS).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 static final int randW() { | |
seed ^= seed << 11; | |
seed ^= seed >>> 25; | |
seed ^= seed << 4; | |
int out = seed % 511; | |
return out < 0 ? -out : out; | |
} | |
private final int[] tmpBuffer = new int[512]; | |
private final byte[] tmpBufferb = new byte[512 * 3]; | |
private final int[] clearBuffer = new int[512 * 512]; | |
private void renderBuffer() { | |
final int gc = Color.GREEN.getRGB(); | |
final int bc = Color.BLUE.getRGB(); | |
switch (test) { | |
case TEST_GRAPHICS: | |
bGraphics.clearRect(0, 0, 512, 512); | |
for (int i = 0; i < 100; i++) { | |
bGraphics.setColor(rand255() > 127 ? Color.GREEN : Color.BLUE); | |
bGraphics.fillRect(0, 0, randW(), randW()); | |
} | |
break; | |
case TEST_DATABUFFER_INT: | |
Arrays.fill(pixelsInt, 0); | |
for (int i = 0; i < 100; i++) { | |
final int w = randW(); | |
final int h = randW(); | |
Arrays.fill(tmpBuffer, 0, w, rand255() > 127 ? gc : bc); | |
for (int y = 0; y < h; y++) | |
System.arraycopy(tmpBuffer, 0, pixelsInt, y * 512, w); | |
} | |
break; | |
case TEST_DATABUFFER_BYTE: | |
Arrays.fill(pixelsByte, (byte) 0); | |
for (int i = 0; i < 100; i++) { | |
final int w = randW(); | |
final int h = randW(); | |
final byte gcr = 0; | |
final byte gcg = (byte) (rand255() > 127 ? 255 : 0); | |
final byte gcb = (byte) (255 - gcg); | |
for (int x = 0; x < w * 3;) { | |
tmpBufferb[x++] = gcb; | |
tmpBufferb[x++] = gcg; | |
tmpBufferb[x++] = gcr; | |
} | |
for (int y = 0; y < h; y++) | |
System.arraycopy(tmpBufferb, 0, pixelsByte, y * 512 * 3, w * 3); | |
} | |
break; | |
case TEST_SETRGB: | |
buffer.setRGB(0, 0, 512, 512, clearBuffer, 0, 512); | |
for (int i = 0; i < 100; i++) { | |
final int w = randW(); | |
final int h = randW(); | |
Arrays.fill(tmpBuffer, 0, w, rand255() > 127 ? gc : bc); | |
for (int y = 0; y < h; y++) | |
buffer.setRGB(0, 0, w, h, tmpBuffer, 0, 1); | |
} | |
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