What is the fastest way to clear a memory block (or SDL surface)?

I am currently developing a project with SDL. It mainly draws and moves images (surfaces) on the screen.

To move an image without leaving a trace, you must first clean the screen surface, much like glClear (), and now I am doing this with a simple loop repeating over the pixels of the surface (also drawing a black box on the surface or in memory).

While the previous solutions work fine on small surfaces, they become slower as the surface grows, so I was looking for the fastest way that I could clear the (zero) block of memory.

In addition, a friend noted that using SIMD instructions can do the job very quickly, but the last time I did ASM was at 8085, any information about this could also be useful.

+6
source share
2 answers

The fastest way is to use memset .

 memset(ptr, 0, length); 

It automatically uses SIMD for architectures that support it * . You will not beat him. It is already tied to memory, so it writes zeros as fast as the processor can spit them out. I don’t know who told you that memset slower for large blocks, but you should stop listening to this person.

* There are several toolkits that do not give you a quick memset . It is unlikely that you are using it.

+13
source

You should try memset , the implementation must be highly optimized to use all the instructions available on your system.

+3
source

Source: https://habr.com/ru/post/904210/


All Articles