我正在制作SDL程序,有时我需要获得矩形的宽度。当我做这样的事情时:
int VideoWidth = Video.Screen.Width; //Expensive operation
int VideoHeight = Video.Screen.Height;
int ChunksRendered = 0;
foreach (Chunk c in Chunks)
{
if (c.X + 256 + DeferredX > 0 && c.X + DeferredX < VideoWidth)
{
if (c.Y + 256 + DeferredY > 0 && c.Y + DeferredY < VideoHeight)
{
BlitSurface.Blit(c.Render(ref Textures, ref BlitSurface), new Point(c.X + DeferredX, c.Y + DeferredY));
ChunksRendered++;
}
}
}
我的机器上有大约550 FPS,但是当我这样做时:
int ChunksRendered = 0;
foreach (Chunk c in Chunks)
{
if (c.X + 256 + DeferredX > 0 && c.X + DeferredX < Video.Screen.Width)
{
if (c.Y + 256 + DeferredY > 0 && c.Y + DeferredY < Video.Screen.Height)
{
BlitSurface.Blit(c.Render(ref Textures, ref BlitSurface), new Point(c.X + DeferredX, c.Y + DeferredY));
ChunksRendered++;
}
}
}
我只能获得200 FPS左右。在分析时,显然这会导致速度变慢,但为什么这个变量如此昂贵?
答案 0 :(得分:5)
可能是Video.Screen
属性访问器导致这种减速。如果您使用的是SdlDotNet.dll
,则以下是该属性的代码:
public static Surface Screen
{
get
{
return Surface.FromScreenPtr(Tao.Sdl.Sdl.SDL_GetVideoSurface());
}
}
这意味着每次访问Surface
属性时都会调用1个外部dll函数并创建1个Video.Screen
对象。
访问Width
的{{1}}或Height
属性也很昂贵,因为它包括调用以下代码:
Surface