Fastest Way To Clear Screen?

Is this the fastest way to clear a screen? Storing a “cleared” screen in another array and System.arraycopying it into the current pixel array?


public final class Display {

	private int[] clearedPixels;
	int[] pixels;
	int width;
	int height;
	private int size;
	
	public Display(int w, int h) {
		this.width = w;
		this.height = h;
		pixels = new int[w * h];
		clearedPixels = new int[w * h];
		size = w * h;
	}

	public void setClearColor(int color) {
		for (int i = 0; i < clearedPixels.length; i++) {
			clearedPixels[i] = color;
		}
	}
	
	public void clear() {
		System.arraycopy(clearedPixels, 0, pixels, 0, size);
	}

}

Try Arrays.fill().

That method is just a for loop with some extra safety precautions that make it even slower. System.arraycopy is native code so it’s lightning fast haha

Be careful with such assumptions within a highly optimizing runtime: http://psy-lob-saw.blogspot.com/2015/04/on-arraysfill-intrinsics-superword-and.html

Ahhh… that’s very interesting thank you for showing that to me!

Along the same lines, just because there’s a classfile that defines method “foo”, you can’t assume that “foo” is ever actually executed. Specifically intrinsic methods. This is getting long in the tooth now…maybe I should update it sometime: http://www.java-gaming.org/topics/hotspot-intrinsics/27010/view.html