在C#中的“新”的性能开销? [英] Performance cost of 'new' in C#?

查看:434
本文介绍了在C#中的“新”的性能开销?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#有什么使用new关键字的性能开销?我关于游戏的发展特别要求,我知道在C ++中它是一个明确的禁忌是newing东西每个更新周期。做同样适用于C#?我使用XNA和移动开发。只是要求作为优化的问题真的。

In C# what is the performance cost of using the new keyword? I ask specifically in relation to games development, I know in C++ it is a definite no-no to be newing things every update cycle. Does the same apply to C#? I'm using XNA and developing for mobile. Just asking as an optimization question really.

推荐答案

有三个部分的成本


  • 分配内存(可能无法如果它是一个值类型所需的)

  • (取决于你在做什么),运行构造

  • 垃圾收集费用(同样,这可能不是如果它是一个值类型,根据上下文应用)

这是很难习惯用法使用C#而没有创造的任何的在你的主代码新对象...虽然我不敢说这是通过为重重用对象尽可能可行的。试图得到一些实际设备的持有,看看你的游戏如何执行。

It's hard to use C# idiomatically without ever creating any new objects in your main code... although I dare say it's feasible by reusing objects as heavily as possible. Try to get hold of some real devices, and see how your game performs.

我当然同意这样的微型优化的一般要避免编程,但它更可能是适当的游戏循环比其他地方 - 因为很明显游戏,甚至小的停顿非常敏感。它可以是非常难以判断使用更多的对象,虽然,因为它传播出去一段时间,由于GC成本的费用。

I'd certainly agree that micro-optimization like this is usually to be avoided in programming, but it's more likely to be appropriate for game loops than elsewhere - as obviously games are very sensitive to even small pauses. It can be quite hard to judge the cost of using more objects though, as it's spread out over time due to GC costs.

分配器和垃圾收集器是相当不错的.NET,虽然它很可能是在设备上简单(Windows Phone 7的,我认为)?尤其是,我不知道该Compact Framework的CLR(这是一个WP7使用)是否具有代GC。

The allocator and garbage collector is pretty good in .NET, although it's likely to be simpler on a device (Windows Phone 7, I assume)? In particular, I'm not sure whether the Compact Framework CLR (which is the one WP7 uses) has a generational GC.

这篇关于在C#中的“新”的性能开销?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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