简体   繁体   English

C#中最重要的优化性能最佳实践是什么

[英]What are the most important optimizing performance best practices in C#

When I was reading this tutorial I noticed the following performance tip about using structs in C#: 当我阅读本教程时,我注意到以下有关在C#中使用结构的性能提示:

Unless you need reference type semantics, a class that is smaller than 16 bytes may be more efficiently handled by the system as a struct. 除非您需要引用类型语义,否则系统可以更有效地将小于16字节的类作为结构进行处理。

I looked for similar question in stackoverflow and I found some questions that talk about performance best practices in ADO.Net , Networking , Streams , but not about performance best practices in C# (The language) . 我在stackoverflow中查找了类似的问题,我发现了一些关于ADO.NetNetworkingStreams中的 性能最佳实践的问题 ,而不是关于C#(该语言)中的性能最佳实践

I want to add another tip about using the integer types: 我想添加关于使用整数类型的另一个提示:

The runtime optimizes the performance of 32-bit integer types (Int32 and UInt32), so use those types for counters and other frequently accessed integral variables. 运行时优化了32位整数类型(Int32和UInt32)的性能,因此将这些类型用于计数器和其他经常访问的整数变量。

Simply: profile. 简单:个人资料。

Every app is different. 每个应用程序都不同。 Taking time to reduce some code to make it "more efficient" is meaningless if that is not a bottleneck in you app. 花时间减少一些代码以使其“更有效”是没有意义的,如果这不是你的应用程序的瓶颈。 Also - you may even be making things worse if you don't have numbers to support changes. 此外 - 如果您没有数字来支持更改,您甚至可能会使事情变得更糟

In most cases IO is the pinch-point, so thinking about IO is a no-brainer. 在大多数情况下,IO是关键点,所以考虑IO是一个明智的选择。 Ditto DB access. 同上DB访问。 But beyond that: measure it. 但除此之外:衡量它。

  1. Strings are Immutable . 字符串是不可改变的
  2. Understand the using statement. 理解using语句。
  3. Understand Boxing and how Generics help. 了解拳击泛型如何帮助。
  4. Understand how the Garbage Collector works. 了解垃圾收集器的工作原理。
  5. Parallel programming in .Net 4.0 .Net 4.0中的并行编程
  6. Understand how File IO affects performance. 了解文件IO如何影响性能。

Eric Lippert talks alot about optimization. Eric Lippert谈论了很多优化问题。 I would read his blog . 我会读他的博客
I would check out Jon Skeet's blog also. 我也会查看Jon Skeet的博客

在非特殊情况下不要使用例外。

Not just in C#, but in any OO language where you are encouraged to make lots of data structure classes, it will probably take some performance tuning and profiling experience to learn this, but keep it simple is more than just a plattitude. 不仅仅是在C#中,而是在鼓励你创建大量数据结构类的任何OO语言中,可能需要一些性能调优和分析经验来学习这一点,但保持简单不仅仅是一个高度。

It is essential to minimize the number of classes you have, minimize the redundancy of the data, and especially minimize the use of notification-style updating to try to keep the data consistent. 必须尽量减少所拥有的类的数量,最大限度地减少数据的冗余,特别是尽量减少使用通知式更新以尽量保持数据的一致性。

If different components of the data structure need to be kept consistent with each other, it is better to be able to tolerate temporary inconsistency than to try, through notifications, to keep things tightly in agreement. 如果数据结构的不同组件需要保持彼此一致,那么最好能够容忍临时不一致,而不是通过通知尝试使事情保持一致。

Many of the complications that are put into data structure arise out of a vague but pervasive desire to make it "more efficient", such as cross-linking data structures so that notifications can implement instantaneous updates. 数据结构中的许多复杂问题源于模糊但普遍的使其“更有效”的愿望,例如交叉链接数据结构,以便通知可以实现即时更新。 Not only does that greatly complicate the code, leading to bugs, but then when you do performance tuning you find out it is those structures that can be the biggest performance-killers. 这不仅会使代码大大复杂化,从而导致错误,而且当您进行性能调优时,您会发现这些结构可能是最大的性能杀手。

Avoid boxing and unboxing in loops. 避免在循环中装箱和拆箱。

For example instead of ArrayList use List. 例如,使用List而不是ArrayList。 Generic collections are not only type-safe but are also faster than non-generics when you use them with value-types. 通用集合不仅类型安全,而且当您将它们与值类型一起使用时,它们也比非泛型集合更快。

I can name a lot of performance optimisations : 我可以说出很多性能优化:

  1. String.Format / StringBuilder for string manipulation as string is immutable. 字符串操作的String.Format / StringBuilder为字符串是不可变的。
  2. Inherit IDisposable to write your own code for disposal of objects, so that you remove unmanaged memory references, and also do not implement IDisposable when you are only working with managed memory. 继承IDisposable以编写自己的代码以处理对象,以便删除非托管内存引用,并且在仅使用托管内存时也不实现IDisposable。
  3. Do not set local references to null, as it is done automatically. 不要将本地引用设置为null,因为它是自动完成的。
  4. Do not create a new exception while you are throw. 在你抛出时不要创建新的异常。 Use throw than throw ex 使用throw比抛出ex
  5. Put large objects as WeakReference, which makes it available to GC immediately. 将大对象作为WeakReference放置,使其立即可用于GC。 http://www.abhisheksur.com/2010/07/garbage-collection-algorithm-with-use.html http://www.abhisheksur.com/2010/07/garbage-collection-algorithm-with-use.html
  6. Avoid Boxing / Unboxing. 避免拳击/拆箱。 Use Generics. 使用泛型。

etc. 等等

In many apps, especially web apps, your app spends most of its time hitting a database. 在许多应用程序中,尤其是Web应用程序中,您的应用程序花费大部分时间来访问数据库。 (This is not C# specific) Use a profiling tool to ensure that you do not have long running db queries, select good indexes, and use eager-loading where appropriate to cut down the number of database requests you have to make. (这不是特定于C#的)使用分析工具来确保您没有长时间运行的数据库查询,选择好的索引,并在适当的地方使用预先加载来减少必须进行的数据库请求的数量。 This is probably the single biggest thing you can do to improve performance of an app that uses a database, C# or otherwise. 这可能是您可以做的最大的事情,以提高使用数据库,C#或其他方式的应用程序的性能。

如果您从对象“B”订阅对象“A”上的事件,请确保在“B”更改之前取消订阅“A”中的“B”事件,否则,“B”可能永远不会得到GC。

Avoid string operations where possible (lookups, .Contains("blah"), etc.) when doing massive amounts of operations. 在进行大量操作时,尽可能避免字符串操作(lookups,.Contains(“blah”)等)。 I have found significant performance enhancements when removing operations like these where possible. 在可能的情况下删除这些操作时,我发现了显着的性能增强。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM