Cls VS DrawBlock VS DrawImage

BlitzPlus Forums/BlitzPlus Programming/Cls VS DrawBlock VS DrawImage

WolRon(Posted 2006) [#1]
I couldn't believe the results I found with this test program. Clearly, clearing the screen with a Cls command is hundreds of times more efficient than drawing 'blank' pixels.

I was testing to see if clearing the screen with a cls command and then redrawing only the upper about 1/3 of the screen would be more efficient than just drawing the entire screen (in one shot) with an image that covered the whole screen but contained 'empty' pixel info (0, 0, 0) in the majority of the image.
The image was something like this (0 represents empty pixels):
0111111111111110
0111111111111110
0000000000000000
0000000000000000
0000000000000000
0000000000000000

Interesting as well, DrawImage actually outperformed DrawBlock by 30%. I always assumed a DrawBlock would be faster since it didn't bother to check for masked pixels, but clearly not drawing the masked pixels at all is faster.

Cls = 6 millisecs
DrawBlock = 3150 millisecs
DrawImage = 2153 millisecs

That makes the Cls command 500 times faster than the DrawBlock command (350 times faster if you subtract 33% for the upper portion of the image being drawn). There must be some kind of Graphics card optimization being used there.

So, by using a Cls, and then only redrawing the top 1/3 of the screen, I was able to save 60% time.

and here's the code I tried:
Graphics 640, 400
;load main menu image
MenuBG = LoadImage("testmenu_bg.png", 4)
Flip
Delay 2000
time = MilliSecs()
For iter = 1 To 1000
	Cls
Next
time2 = MilliSecs()
For iter = 1 To 1000
	DrawBlock MenuBG, 0, 0
Next
time3 = MilliSecs()
For iter = 1 To 1000
	DrawImage MenuBG, 0, 0
Next
time4 = MilliSecs()


Cls
Text 0, 0, time2-time
Text 0, 30, time3-time2
Text 0, 60, time4-time3
Flip
WaitKey
End


- - - EDIT - - -
Oops, I realized now that the majority of the speed reductions was due to my loading the image as a type 4 image due to it being used as a title screen.
So, the stats still apply, but only to a type 4 image. When I loaded it as a type 1 (the default), then my test results weren't as significant, but still showed some improvement.

Cls = 6 millisecs
DrawBlock = 225 millisecs
DrawImage = 126 millisecs

That's still 37 times faster than the DrawBlock command.

And to show comparison, the same test program with images loaded as type 2s:

Cls = 6 millisecs
DrawBlock = 168 millisecs
DrawImage = 179 millisecs

DrawImage isn't any more efficient anymore with a type 2.


Grey Alien(Posted 2006) [#2]
With type 4, scratch, the delay is the entire image going across to the video card from RAM each time. With type 1, managed, your very first drawblock will take twice as long as the rest of the drawblocks (test it) because it's being copied to the VRAM from RAM, then each subsequent use will be quicker cos it's using the VRAM copy (unless you modify the image, in which a case a re-copy to VRAM is required). Type 2 is loaded straight onto the VRAM so it shouldn't be any slower the first time. It's odd that the drawimage stat for Type2 is slower than Type1, this doesn't make sense. I have found that with these sort of benchmarks you need to leave a decent delay at the start before commencing otherwise they can vary a bit. Also did you run the test more than once for each type to check the results were always in the same range?

As a matter of interest, I would always use CLS anyway :-) but I see that you were only needing to clear a portion of the screen and thought you'd optimise. Clearly the video card has some kind of hardware clearing OR optimisation i.e. it only bothers doing it once! (try drawing a single pixel after each CLS and see if it slows down).

Would be interesting to try the same test on an Image in RAM (scratch) by pointing at it with ImageBuffer(testimage), not on the card, and see how fast CLS is then ...


Buggy(Posted 2006) [#3]
I'm a little confused how you can say that cls is 37 times faster than drawblock, because although that may be so, with cls, you need to use drawimage also, so really it's only marginally faster.

But this info still helps a lot.

By the way, what's a type 4 image?


WolRon(Posted 2006) [#4]
I tried to make myself understood in the first post. I was clearing the screen with an image that was 2/3 just black pixels. I thought it would be faster than performing a Cls and then performing a DrawBlock or DrawImage (because that's two commands) on the upper 1/3 of the screen.

I found out that using a Cls takes almost no time at all (remember, the 6 millisecs is 1000 Cls's), so there's nothing to lose by using a Cls and then RE-drawing over the same cleared screen with an image.


Buggy(Posted 2006) [#5]
6 millisex is 1000 Cls'? Ooh... that makes more sense.

Still a little confused, but no matter. So what is a type 4, type 1, or type 2 or 3 image?


skidracer(Posted 2006) [#6]
I found out that using a Cls takes almost no time at all


To be quite correct, it takes no time at all to issue a CLS command to the graphics card, which will then most likely buffer it up with all the other drawing stuff it is required to do in it's own time...

Try locking and releasing the backbuffer before calling millisecs which shoulld guarantee the drawing has actually been accomplished.


Grey Alien(Posted 2006) [#7]
Wolron: Yeah like I said it probably only does the CLS once so the test isn't correct.

Buggy. Type Loadimage into the Ide and press F1 on it. The flag controls the image type. 1,2 and 4 are the different types, 1 is stored in RAM and VRAM, 2 in vRAM only, 4 in RAM only. The VRAM one will corrup if you alt+tab out and then back into your game.


Buggy(Posted 2006) [#8]
What?! How do I press F1 on something? I no nothing of your computer jargon!


WolRon(Posted 2006) [#9]
Are you joking?


Grey Alien(Posted 2006) [#10]
sorry I just gotta lol, no offence. anyway, type Loadimage into the IDE (the BlitzPlus compiler) and move the cursor into the middle of the word, then press F1. The help for that keyword will appear! tada!


Buggy(Posted 2006) [#11]
Really?!

...*Three and a half hours later*...

Wow! It does work! It's amazing!

Thanks a lot.