这些 C# 功能对性能有何影响? [英] What are the performance implications of these C# features?

查看:41
本文介绍了这些 C# 功能对性能有何影响?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在设计一个基于组件的游戏库,总体意图是用 C++ 编写它(因为这是我的强项),以 Ogre3D 作为后端.现在我实际上已经准备好编写一些代码,我认为在 XNA4.0 框架下测试我的框架会快得多(获得结果/编写编辑器等会更快一些).然而,虽然我不是 C++ 或 C# 的新手,但在以XNA"方式做事时,我有点新手,可以这么说,所以在我开始之前我有一些疑问开始敲定代码:

I have been designing a component-based game library, with the overall intention of writing it in C++ (as that is my forte), with Ogre3D as the back-end. Now that I am actually ready to write some code, I thought it would be far quicker to test out my framework under the XNA4.0 framework (somewhat quicker to get results/write an editor, etc). However, whilst I am no newcomer to C++ or C#, I am a bit of a newcomer when it comes to doing things the "XNA" way, so to speak, so I had a few queries before I started hammering out code:

  1. 我读过关于使用数组而不是集合来避免性能下降的内容,然后还读到这并不完全正确,如果你枚举一个具体的 List<>集合(相对于 IEnumerable<>),枚举器是一个值类型,用于每次迭代并且这里没有任何 GC 问题.有问题的文章是在 2007 年.这是否成立,或者您是否有经验的 XNA 开发人员对此有实际问题?理想情况下,我希望在我做太多事情之前沿着选定的路线走下去.

  1. I read about using arrays rather than collections to avoid performance hits, then also read that this was not entirely true and that if you enumerated over, say, a concrete List<> collection (as opposed to an IEnumerable<>), the enumerator is a value-type that is used for each iteration and that there aren't any GC worries here. The article in question was back in 2007. Does this hold true, or do you experienced XNA developers have real-world gotchas about this? Ideally I'd like to go down a chosen route before I do too much.

如果数组真的是可行的方法,那么毫无疑问,我假设在调整数组大小时,您会用新空间复制旧数组吗?还是这不合时宜?您是否尝试过从不调整数组大小?如果是这种情况,GC 会不会为旧的启动,或者命中无关紧要?

If arrays truly are the way to go, no questions asked, I assume when it comes to resizing the array, you copy the old one over with new space? Or is this off the mark? Do you attempt to never, ever resize an array? Won't the GC kick in for the old one if this is the case, or is the hit inconsequential?

由于引擎是为 C++ 设计的,因此该设计允许使用 lambda 表达式和委托.一种设计使用 fastdelegate 库,这是在 C++ 中使用委托的最快方式.一种更灵活但速度稍慢的方法(虽然在 C++ 世界中几乎不明显)是使用 C++0x lambdas 和 std::function.理想情况下,我想在 XNA 中做类似的事情,并允许使用委托.使用委托是否会导致性能方面的任何重大问题?

As the engine was designed for C++, the design allows for use of lambdas and delegates. One design uses the fastdelegate library which is the fastest possible way of using delegates in C++. A more flexible, but slightly slower approach (though hardly noticeable in the world of C++) is to use C++0x lambdas and std::function. Ideally, I'd like to do something similar in XNA, and allow delegates to be used. Does the use of delegates cause any significant issues with regard to performance?

如果有关于委托的性能考虑,是否有区别:

If there are performance considerations with regards to delegates, is there a difference between:

public void myDelegate(int a, int b);
private void myFunction(int a, int b)
{
}

event myDelegate myEvent;

myEvent += myFunction;

对比:

    public void myDelegate(int a, int b);

    event myDelegate myEvent;

    myEvent += (int a, int b) => { /* ... */ };

对不起,如果我有点胡说八道,我更愿意在我的问题中说清楚.:)

Sorry if I have waffled on a bit, I prefer to be clear in my questions. :)

提前致谢!

推荐答案

基本上,在 C# 中唯一需要注意的与在 C++ 中必须注意的主要性能问题是垃圾收集器.简单地不要在主游戏循环期间分配内存,你会没事的.这里是一篇详细介绍的博文.

Basically the only major performance issue to be aware of in C# that is different to what you have to be aware of in C++, is the garbage collector. Simply don't allocate memory during your main game loop and you'll be fine. Here is a blog post that goes into detail.

现在回答您的问题:

1) 如果框架集合迭代器可以实现为值类型(不创建垃圾),那么它通常(总是?)一直是.例如,您可以安全地在 List<> 上使用 foreach.

1) If a framework collection iterator could be implemented as a value-type (not creating garbage), then it usually (always?) has been. You can safely use foreach on, for example, List<>.

您可以使用 CLR 分析器.

2) 使用 List 代替数组.他们会为您处理调整大小.您应该在开始游戏之前使用 Capacity 属性预先分配足够的空间以避免 GC 问题.使用数组你只需要自己实现所有这些功能 - 丑陋!

2) Use Lists instead of arrays. They'll handle the resizing for you. You should use the Capacity property to pre-allocate enough space before you start gameplay to avoid GC issues. Using arrays you'd just have to implement all this functionality yourself - ugly!

GC 开始分配(不是在内存空闲时).在 Xbox 360 上,它每分配 1MB 就会启动,而且速度非常慢.在 Windows 上,它有点复杂 - 但也不会对性能产生如此大的影响.

The GC kicks in on allocations (not when memory becomes free). On Xbox 360 it kicks in for every 1MB allocated and is very slow. On Windows it is a bit more complicated - but also doesn't have such a huge impact on performance.

3) C# 委托非常快.并且比大多数人预期的要快.它们与接口上的方法调用相当.这里此处 提供了有关 C# 中委托性能的更多详细信息的问题.

3) C# delegates are pretty damn fast. And faster than most people expect. They are about on-par with method calls on interfaces. Here and here are questions that provide more detials about delegate performance in C#.

我无法说出它们与 C++ 选项相比如何.你必须测量它.

I couldn't say how they compare to the C++ options. You'd have to measure it.

4) 不.我很确定这段代码会产生相同的 IL.不过,您可以拆开它并检查或分析它.

4) No. I'm fairly sure this code will produce identical IL. You could disassemble it and check, or profile it, though.

我可能会添加 - 不检查自己 - 如果您不需要 myDelegate 的所有魔法,我怀疑拥有 event myDelegate 会比普通的 myDelegate 慢代码>事件.

I might add - without checking myself - I suspect that having an event myDelegate will be slower than a plain myDelegate if you don't need all the magic of event.

这篇关于这些 C# 功能对性能有何影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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