Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This work is licensed under a Creative Commons Attribution 3.0 Unported License, available
- * at http://creativecommons.org/licenses/by/3.0/
- *
- * Please attribute by not editing the @author lines in any location on the source code.
- * Last modified 1/6/2013
- */
- import java.awt.Frame;
- import java.text.NumberFormat;
- import javax.swing.JFrame;
- /**
- *
- * @author /u/Tjstretchalot
- */
- public class CanvasVsJComponentTest {
- /**
- * Performs a speed check on canvas and jcomponent
- * @param arg unused
- * @throws InterruptedException
- */
- public static void main(String[] args) throws InterruptedException {
- // JComponent with double buffering
- boolean doubleBuf = true;
- JComponentTest myJComponent = new JComponentTest(doubleBuf);
- JFrame frame = new JFrame();
- frame.add(myJComponent);
- frame.setSize(250, 250);
- frame.setTitle("JComponent Test");
- frame.setVisible(true);
- // Warm up the vm
- for(int i = 0; i < 1000; i++) {
- myJComponent.paintImmediately(0, 0, 250, 250);
- }
- System.gc();
- Thread.sleep(500);
- System.gc();
- //Begin testing
- final int frames = 100000;
- long start = System.currentTimeMillis();
- for(int i = 0; i < frames; i++) {
- myJComponent.paintImmediately(0, 0, 250, 250);
- }
- long end = System.currentTimeMillis();
- long time = end - start;
- double seconds = time / 1000.0;
- double fps = frames / seconds;
- System.out.println("JComponent with" + (!doubleBuf ? "out " : " ") + "buffering: " + round(fps) + " fps");
- frame.setVisible(false);
- myJComponent = null;
- frame = null;
- System.gc();
- Thread.sleep(500);
- System.gc();
- // Rewarm up, to be consistent
- CanvasTest myCanvas = new CanvasTest();
- Frame canvasFrame = new Frame("Canvas Test");
- canvasFrame.setSize(250, 250);
- canvasFrame.add(myCanvas);
- canvasFrame.setVisible(true);
- myCanvas.setDoubleBuffered(doubleBuf);
- for(int i = 0; i < 1000; i++) {
- myCanvas.render();
- }
- System.gc();
- Thread.sleep(500);
- System.gc();
- start = System.currentTimeMillis();
- for(int i = 0; i < frames; i++) {
- myCanvas.render();
- }
- end = System.currentTimeMillis();
- time = end - start;
- seconds = time / 1000.0;
- fps = frames / seconds;
- canvasFrame.setVisible(false);
- System.out.println("Canvas with" + (!doubleBuf ? "out " : " ") + "buffering: " + round(fps) + " fps");
- System.exit(1);
- }
- private static String round(double fps) {
- NumberFormat myFormat = NumberFormat.getInstance();
- return myFormat.format(fps);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement