80 FPS in Java? Yes!

Posted by devon on July 5, 2011 in Java |

This comes from a recent question on StackOverflow where the poster asked whether it is possible to paint a 1024×768 window at 30fps. The answer is an enthusiastic Yes!.

On my desktop this baby clocks in at over 80 FPS!

The single, largest gain comes from the use of MemoryImageSource instead of a simple BufferedImage. Writing data into a BI can be done with setRGB(x, y, value), but this requires 786,432 method calls!

If you buffer a lines worth at a time, you can use
setRGB(startX, startY, width, height, buffer, offset, scanlineLength), which is much faster, but still requires 768 function calls.


  • The ordering of inner loops in doInBackground() is deliberate. Had we looped over x, then over y, the value of the calculated index would have had this pattern: [0 1024 2048 … 1 1025 2049 … 2 1026 2050 …]. The non-sequential memory accesses would result in a cache miss on every write into mem.


Copyright © 2011-2017 Experimental.Jar All rights reserved.
This site is using the Desk-Mess-Mine Child-Theme, v1.0.0, on top of
the Parent-Theme Desk Mess Mirrored, v2.5, from BuyNowShop.com