gpt4 book ai didi

c# - 为什么 System.Drawing.Rectangle.getWidth() 如此昂贵?

转载 作者:行者123 更新时间:2023-12-02 22:02:10 25 4
gpt4 key购买 nike

我正在制作一个 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。分析时很明显这会导致速度变慢,但为什么获取此变量如此昂贵?

最佳答案

可能是 Video.Screen 属性访问器导致了这种减速。如果您使用的是 SdlDotNet.dll,这里是该属性的代码:

public static Surface Screen
{
get
{
return Surface.FromScreenPtr(Tao.Sdl.Sdl.SDL_GetVideoSurface());
}
}

这意味着每次访问 Video.Screen 属性时都会调用 1 个外部 dll 函数并创建 1 个 Surface 对象。

访问 SurfaceWidthHeight 属性也很昂贵,因为它包括调用以下代码:

internal Tao.Sdl.Sdl.SDL_Surface SurfaceStruct
{
get
{
if (this.disposed)
{
throw new ObjectDisposedException(this.ToString());
}
GC.KeepAlive(this);
return (Tao.Sdl.Sdl.SDL_Surface) Marshal.PtrToStructure(base.Handle, typeof(Tao.Sdl.Sdl.SDL_Surface));
}
}

关于c# - 为什么 System.Drawing.Rectangle.getWidth() 如此昂贵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16755298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com