I'm currently developing a project with SDL. It basically draws and moves images (surfaces) on the screen.
我目前正在开发一个SDL项目。它基本上在屏幕上绘制和移动图像(表面)。
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).
要在不留下痕迹的情况下移动图像,必须首先清除屏幕表面,非常像glClear(),我现在正在使用一个简单的for循环迭代表面的像素(也在表面上绘制一个黑盒子)或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.
另外,一位朋友指出,使用SIMD指令可以非常快地完成工作,但是我上次完成ASM的时间是8085,对此的任何见解也可能有用。
2 个解决方案
#1
13
The fastest way is to use memset
.
最快的方法是使用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.
这会在支持它的架构上自动使用SIMD *。你不会打败它。它已经受到内存限制,所以它写入零的速度和处理器可以吐出的速度一样快。我不知道是谁告诉你memset对于较大的块来说速度较慢,但你应该停止听那个人。
*There are some toolchains that don't give you a fast memset
. It is unlikely that you are using one.
*有一些工具链不能给你快速的memset。你不太可能使用它。
#2
3
You should try memset
, the implementation should be highly optimized to take advantage of whatever instructions are available on your system.
您应该尝试使用memset,应该高度优化实现,以利用系统上可用的任何指令。
#1
13
The fastest way is to use memset
.
最快的方法是使用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.
这会在支持它的架构上自动使用SIMD *。你不会打败它。它已经受到内存限制,所以它写入零的速度和处理器可以吐出的速度一样快。我不知道是谁告诉你memset对于较大的块来说速度较慢,但你应该停止听那个人。
*There are some toolchains that don't give you a fast memset
. It is unlikely that you are using one.
*有一些工具链不能给你快速的memset。你不太可能使用它。
#2
3
You should try memset
, the implementation should be highly optimized to take advantage of whatever instructions are available on your system.
您应该尝试使用memset,应该高度优化实现,以利用系统上可用的任何指令。