XNA游戏性能 [英] XNA game performance

查看:69
本文介绍了XNA游戏性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 C# 2010 和 XNA 4.0 编写游戏.我希望它是一款出色的游戏,而不是一些只是另一个废话",所以我的目标之一就是良好的帧率.为此,我想请教您一些建议,无论是与 XNA 相关还是与 C# 相关 - 我可以做些什么来加速我的代码并提高 FPS.

I am writing a game using C# 2010 and XNA 4.0. I want it to be a good game and not some "just another crap" and so one of my goals is good framerate. For this I would like to ask you for some advices, be it XNA related or C# related - what can I do to speed up my code and so improve FPS.

这是我发现的一些东西:

Here is some stuff I found out:

  • (c#) 使用 array 插入 list<> 将大大提高性能,以防您想访问大量数据(甚至很多:20000 个项目的数组提供了 20000 个列表的近 180% 的 FPS.
  • (c#) 使用 for 而不是 foreach 将提高性能.在相同的 20000 个元素上,它就像 5-10% 的差异.
  • (xna 和 c#)调用方法会降低性能,因此当我将代码分散到调用其他方法等的方法中时,一切都非常酷且面向对象,每次调用的成本为 1-3 FPS.
  • (xna) update vs. draw:当我把代码放入update()和draw()时,性能没有区别,所以如果你不太在意,你不必把一些方法放在update()下而不是将所有内容都放在 draw() 中以期获得更好的 FPS.
  • (xna) 不知道是我还是 4.0,但是当您加载包含大量纹理和非常复杂的网格的 .X 时,加载需要很多时间.我不得不编写自己的 .X 加载器和网格/模型渲染器,因为 XNA 开始为每个三角形加载纹理,而不管前一个是否具有相同的纹理.即使编写我自己的为您缓存纹理的内容加载器也不是解决方案,因为它仍然被使用了很多次.如果你制作的东西既漂亮又蓬松,我建议使用 drawuserprimitives 而不是 Model 类.(不知道是 4.0 还是 3.1 也有这样的性能问题)
  • (xna 和 c#)在绘图时尽量避免使用new".我在网上看到了一些代码,什么是复制/粘贴在一起的,并且有basicEffect = new BasicEffect(graphicsDevice);"以及 draw() 中的丑陋之处,更糟糕的是:在 draw() 内部的迭代中.这样会杀死性能.而不是在游戏"或静态类中有一个这样的类.这也适用于其他临时对象":丑陋但我认为最好创建一些全局性的东西并注意使用它,然后编写一个漂亮而蓬松的代码,因为垃圾收集器而使我们的游戏变得如此.
  • (c#) using array insted of list<> will improve performance a lot, in case you want to access huge amount of data (or even lots: having an array of 20000 items gave almost 180% FPS of list of 20000.
  • (c#) using for instead of foreach will improve performance. On same 20000 elements it's like 5-10% difference.
  • (xna and c#) calling methods costs performance, so when I scattered my code into methods what called other methods and such, and everything was so cool and object oriented it was on the cost of 1-3 FPS each call.
  • (xna) update vs. draw: it was no difference in performance when I put code into update() and draw(), so if you do not care much, you do not have to put some methods under update() instead of keeping everything together in draw() in hope of better FPS.
  • (xna) no idea if it is me or just 4.0, but when you load a .X with lot of textures in it and pretty complex meshes, it takes a LOT of time to load. I had to write my own .X loader and mesh/model renderer just because XNA started to load texture for each and every triangle regardless if the previous one had the same texture to it. Even writing my own contentloader which caches textures for you is no solution, because it is still utilised a lot of times. If you make something nice and fluffy I suggest using drawuserprimitives and not Model class. (no idea if it is 4.0 only, or 3.1 had such performance problem too)
  • (xna and c#) Try to avoid using "new" when it is about drawing. I saw some code around on web, what was copy/pasted together and had "basicEffect = new BasicEffect(graphicsDevice);" and such uglyness in the draw(), and even worse: in iterations inside draw(). Such will kill performance. Instead of this have one such class in "game" or in a static class. This applies for other "temp-objects" too: ugly but I think it is better to create something global and use it with attention then to make a nice and fluffy code what will make our game because of garbage collector.

好的,长话短说,请在这里发表一些好的建议,这样我/我们就可以制作出优秀、快速和优化的游戏;)

Ok, so to make long story short, please post some good advice here, so I / we can make good, fast and optimized games ;)

提前致谢:泽克什

推荐答案

UberGeekGames 有一篇关于 XNA 优化的好文章.它主要针对 Xbox 和 WP7,但许多内容也适用于 PC.

UberGeekGames has a good article on XNA optimization. It's mostly targeted at Xbox and WP7, but many of the things also apply to PC.

http://www.sgtconker.com/2011/05/high-end-performance-optimizations-on-the-xbox-360-and-windows-phone-7/罢工>

原始站点已死,但可在 Internet Archive 中找到:

Original site is dead, but is available in the Internet Archive:

xbox 360 和 windows phone7 上高端性能优化的存档版本

经验法则是优化前的配置文件".

The rule of thumb is "profile before optimizing".

这篇关于XNA游戏性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆