Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

assembly

sdl

I'm currently developing a project with SDL. It basically draws and moves images (surfaces) on the screen.

To move an image without leaving a trail, you must first clear the screen surface, pretty much like glClear(), and I'm currently doing it with a simple for loop iterating over the surface's pixels (also drawing a black box on the surface or memset).

While the previous solutions work fine for small surfaces, they get increasingly slower as the surface grows bigger so i was looking for the fastest way I could clear (zero) a memory block.

Also, a friend pointed out that using SIMD instructions could do the work really fast but the last time I've done ASM was on a 8085, any insight on this could also be useful.

like image 611
NeonMan Avatar asked Dec 21 '11 01:12

NeonMan


People also ask

What is the fastest way to clear RAM usage?

RAM Hogs: Low Hanging Fruit The fastest and easiest way to clear up memory that’s being used is to make sure there are no system processes consuming all the system resources. This is an easy problem to develop over time, especially if you’re in the habit of installing a lot of software. There’s also a very easy solution.

How to clear memory and boost RAM Windows 7?

7 Ways to Clear Memory and Boost RAM on Windows. 1 1. RAM Hogs: Low Hanging Fruit. The fastest and easiest way to clear up memory that’s being used is to make sure there are no system processes ... 2 2. Clean Up Startup Programs. 3 3. Clear Page File at Shutdown. 4 4. Check for Device Driver Issues. 5 5. Reduce Windows Visual Effects. More items

How can I maximize the lifespan of my memory card?

That said, there are a few things we can do to maximize the lifespan of our memory cards, including deleting our photos off the memory card in the most efficient manner possible. This isn’t something that’s talked about all that often, but when it comes down to it, it is really simple.

How do I clear space on my hard drive without losing data?

Deleting Unused Files and Applications Delete old files. Delete any applications that you don’t need. Delete everything in your trash, as the files in trash still take up space on your computer. Restart your computer so the hard drive will refresh and cleanse itself to your saved actions.


1 Answers

The fastest way is to use memset.

memset(ptr, 0, length);

This automatically uses SIMD on architectures that support it*. You are not going to beat it. It is already memory bound, so it's writing zeroes as fast as the processor can spit them out. I don't know who told you that memset is slower for larger blocks, but you should stop listening to that person.

*There are some toolchains that don't give you a fast memset. It is unlikely that you are using one.

like image 60
Dietrich Epp Avatar answered Sep 22 '22 15:09

Dietrich Epp