Advertisement
Guest User

The Tester

a guest
Jan 6th, 2013
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. /*
  2.  * This work is licensed under a Creative Commons Attribution 3.0 Unported License, available
  3.  * at http://creativecommons.org/licenses/by/3.0/
  4.  *
  5.  * Please attribute by not editing the @author lines in any location on the source code.
  6.  * Last modified 1/6/2013
  7.  */
  8. import java.awt.Frame;
  9. import java.text.NumberFormat;
  10.  
  11. import javax.swing.JFrame;
  12.  
  13. /**
  14.  *
  15.  * @author /u/Tjstretchalot
  16.  */
  17. public class CanvasVsJComponentTest {
  18.  
  19.     /**
  20.      * Performs a speed check on canvas and jcomponent
  21.      * @param arg unused
  22.      * @throws InterruptedException
  23.      */
  24.     public static void main(String[] args) throws InterruptedException {
  25.         // JComponent with double buffering
  26.         boolean doubleBuf = true;
  27.         JComponentTest myJComponent = new JComponentTest(doubleBuf);
  28.        
  29.         JFrame frame = new JFrame();
  30.         frame.add(myJComponent);
  31.         frame.setSize(250, 250);
  32.         frame.setTitle("JComponent Test");
  33.         frame.setVisible(true);
  34.         // Warm up the vm
  35.         for(int i = 0; i < 1000; i++) {
  36.             myJComponent.paintImmediately(0, 0, 250, 250);
  37.         }
  38.         System.gc();
  39.         Thread.sleep(500);
  40.         System.gc();
  41.         //Begin testing
  42.         final int frames = 100000;
  43.         long start = System.currentTimeMillis();
  44.        
  45.         for(int i = 0; i < frames; i++) {
  46.             myJComponent.paintImmediately(0, 0, 250, 250);
  47.         }
  48.        
  49.         long end = System.currentTimeMillis();
  50.         long time = end - start;
  51.         double seconds = time / 1000.0;
  52.        
  53.         double fps = frames / seconds;
  54.         System.out.println("JComponent with" + (!doubleBuf ? "out " : " ") + "buffering: " + round(fps) + " fps");
  55.        
  56.         frame.setVisible(false);
  57.         myJComponent = null;
  58.         frame = null;
  59.         System.gc();
  60.         Thread.sleep(500);
  61.         System.gc();
  62.        
  63.         // Rewarm up, to be consistent
  64.        
  65.         CanvasTest myCanvas = new CanvasTest();
  66.         Frame canvasFrame = new Frame("Canvas Test");
  67.         canvasFrame.setSize(250, 250);
  68.         canvasFrame.add(myCanvas);
  69.         canvasFrame.setVisible(true);
  70.         myCanvas.setDoubleBuffered(doubleBuf);
  71.        
  72.         for(int i = 0; i < 1000; i++) {
  73.             myCanvas.render();
  74.         }
  75.         System.gc();
  76.         Thread.sleep(500);
  77.         System.gc();
  78.         start = System.currentTimeMillis();
  79.        
  80.         for(int i = 0; i < frames; i++) {
  81.             myCanvas.render();
  82.         }
  83.        
  84.         end = System.currentTimeMillis();
  85.         time = end - start;
  86.         seconds = time / 1000.0;
  87.         fps = frames / seconds;
  88.         canvasFrame.setVisible(false);
  89.         System.out.println("Canvas with" + (!doubleBuf ? "out " : " ") + "buffering: " + round(fps) + " fps");
  90.         System.exit(1);
  91.     }
  92.  
  93.     private static String round(double fps) {
  94.         NumberFormat myFormat = NumberFormat.getInstance();
  95.         return myFormat.format(fps);
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement