What are the most important optimizing performance best practices in C# [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this questionWhen I was reading this tutorial I noticed the following performance tip about using structs in 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.
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).
I want to add another tip about using the integer types:
The runtime optimizes the perfor开发者_运维知识库mance of 32-bit integer types (Int32 and UInt32), so use those types for counters and other frequently accessed integral variables.
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. Ditto DB access. But beyond that: measure it.
- Strings are Immutable.
- Understand the using statement.
- Understand Boxing and how Generics help.
- Understand how the Garbage Collector works.
- Parallel programming in .Net 4.0
- Understand how File IO affects performance.
Eric Lippert talks alot about optimization. I would read his blog.
I would check out Jon Skeet's blog also.
- Always have a performance budget - what level of performance are you (or your customers) looking for in rough terms?
- Measure just about everything performance-related.
- Think about Locality of reference.
- Almost never have finalizers.
- Almost never call GC.Collect.
- Avoid mid-life crisis.
- Understand the cost of exceptions.
- If you’re using an invasive profiler, don’t believe the times it gives you.
- Be aware that Amdahl's Law doesn't always apply.
- Caching may not give you the performance benefits you're looking for.
Don't use exceptions in non-exceptional circumstances.
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.
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. Generic collections are not only type-safe but are also faster than non-generics when you use them with value-types.
In many apps, especially web apps, your app spends most of its time hitting a database. (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. This is probably the single biggest thing you can do to improve performance of an app that uses a database, C# or otherwise.
If you subscribe to an event on object "A" from object "B", be sure to unsubscribe from "B"'s events in "A" before "B" changes, otherwise, "B" may never get GC'd.
Avoid string operations where possible (lookups, .Contains("blah"), etc.) when doing massive amounts of operations. I have found significant performance enhancements when removing operations like these where possible.
I can name a lot of performance optimisations :
- String.Format / StringBuilder for string manipulation as string is immutable.
- 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.
- Do not set local references to null, as it is done automatically.
- Do not create a new exception while you are throw. Use throw than throw ex
- Put large objects as WeakReference, which makes it available to GC immediately. http://www.abhisheksur.com/2010/07/garbage-collection-algorithm-with-use.html
- Avoid Boxing / Unboxing. Use Generics.
etc.
精彩评论