加速 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/183033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:57:03  来源:igfitidea点击:

Speeding Up C#

c#optimizationperformance

提问by akdom

This is really two questions, but they are so similar, and to keep it simple, I figured I'd just roll them together:

这确实是两个问题,但它们非常相似,为了简单起见,我想我会把它们放在一起:

  • Firstly: Given an established C# project, what are some decent ways to speed it up beyond just plain in-code optimization?

  • Secondly: When writing a program from scratch in C#, what are some good ways to greatly improve performance?

  • 首先:给定一个已建立的 C# 项目,除了简单的代码优化之外,还有哪些不错的方法可以加快它的速度?

  • 其次:当用C#从头开始编写程序时,有哪些可以大大提高性能的好方法?

Please stay away from general optimization techniques unless they are C# specific.

请远离通用优化技术,除非它们是C# 特定的

This has previously been asked for Python, Perl, and Java.

以前在PythonPerlJava 中要求这样做。

回答by leppie

NGEN will help with some code, but do not bank on it.

NGEN 将帮助处理一些代码,但不要依赖它。

Personally, if your design is bad/slow, there is not much you can do.

就个人而言,如果您的设计很差/很慢,那么您无能为力。

The best suggestion in such a case, is to implement some form of caching of expensive tasks.

在这种情况下,最好的建议是对昂贵的任务实施某种形式的缓存。

回答by Joel Coehoorn

A lot of slowness is related to database access. Make your database queries efficient and you'll do a lot for your app.

许多缓慢与数据库访问有关。使您的数据库查询高效,您将为您的应用程序做很多事情。

回答by Konrad Rudolph

Off the top of my head:

在我的头顶:

  • Replace non-generic variants of container classes by their generic counterparts
  • Cut down on boxing/unboxing. Specifically, use generics where possible and generally avoid passing value types as object.
  • For dialogs using many dynamic controls: suspend drawing until after inserting all controls by using SuspendLayout/ResumeLayout. This helps especially when using layout containers.
  • 用它们的通用对应物替换容器类的非通用变体
  • 减少装箱/拆箱。具体来说,在可能的情况下使用泛型,并且通常避免将值类型作为object.
  • 对于使用许多动态控件的对话框:在使用SuspendLayout/插入所有控件之前暂停绘图ResumeLayout。这在使用布局容器时尤其有用。

回答by Ben Hoffstein

One simple thing is to ensure that your build configuration is set to "Release". This will enable optimizations and eliminate debugging information, making your executable smaller.

一件简单的事情是确保您的构建配置设置为“发布”。这将启用优化并消除调试信息,使您的可执行文件更小。

More info on MSDNif needed.

如果需要,请访问 MSDN 上的更多信息。

回答by lluismontero

I recomend you those books:

我向你推荐这些书:

Effective C#.

有效的 C#

More Effective C#

更有效的 C#

回答by chrissie1

Don't use to much reflection.

不要使用太多的反射。

回答by Jon Skeet

Unfortunately, relatively few optimisations are language specific. The basics apply across languages:

不幸的是,相对较少的优化是特定于语言的。基础知识适用于各种语言:

  • Measure performance against realistic loads
  • Have clearly-defined goals to guide you
  • Use a good profiler
  • Optimise architecture/design relatively early
  • Only micro-optimise when you've got a proven problem
  • 根据实际负载测量性能
  • 有明确的目标来指导你
  • 使用一个好的分析器
  • 相对较早地优化架构/设计
  • 只有在您遇到已证实的问题时才进行微观优化

When you've absolutely proved you need to micro-optimise, the profiler tends to make it obvious what to look for - things like avoiding boxing and virtual calls.

当您完全证明需要进行微优化时,分析器往往会明确指出要查找的内容 - 例如避免拳击和虚拟调用。

Oh, one thing I can think of which is .NET-specific: if you need to make a call frequently and are currently using reflection, convert those calls into delegates.

哦,我能想到的一件事是 .NET 特定的:如果您需要经常进行调用并且当前正在使用反射,请将这些调用转换为 delegates

EDIT: The other answers suggesting using generics and StringBuilder etc are of course correct. I (probably wrongly) assumed that those optimisations were too "obvious" ;)

编辑:建议使用泛型和 StringBuilder 等的其他答案当然是正确的。我(可能错误地)认为这些优化太“明显”;)

回答by Jeff B

  • Use StringBuilder rather than lots of string concatenation. String objects are atomic, and anymodification (appending, to-upper, padding, etc) actually generate a completely new string object rather than modifying the original. Each new string must be allocated and eventually garbage collected.

  • A generalization of the prior statement: Try to reuse objects rather than creating lots and lots of them. Allocation and garbage collection may be easy to do, but they hit your performance.

  • Be sure to use the provided Microsoft libraries for most things. The classes provided by the Framework often use features that are unavailable or difficult to access from your own C# code (i.e. making calls out to the native Windows API). The built-in libraries are not alwaysthe most efficient, but more often than not.

  • Writing asynchronous apps has never been easier. Look into things like the BackgroundWorker class.

  • Try not to define Structs unless you reallyneed them. Class instance variables each hold a reference to the actual instance, while struct instance variables each hold a separate copy.

  • 使用 StringBuilder 而不是大量的字符串连接。字符串对象是原子的,任何修改(追加、向上、填充等)实际上都会生成一个全新的字符串对象,而不是修改原始对象。每个新字符串都必须被分配并最终被垃圾回收。

  • 对先前声明的概括:尝试重用对象而不是创建大量对象。分配和垃圾收集可能很容易做到,但它们会影响您的性能。

  • 对于大多数事情,请务必使用提供的 Microsoft 库。框架提供的类通常使用从您自己的 C# 代码(即调用本机 Windows API)不可用或难以访问的功能。内置库并不总是最有效的,但往往不是。

  • 编写异步应用程序从未如此简单。查看诸如 BackgroundWorker 类之类的东西。

  • 尽量不要定义结构,除非你真的需要它们。类实例变量每个都持有对实际实例的引用,而结构实例变量每个都持有一个单独的副本。

回答by Gary Willoughby

Use Ngen.exe (Should come shipped with Visual Studio.)

使用 Ngen.exe(应随 Visual Studio 一起提供。)

http://msdn.microsoft.com/en-us/library/6t9t5wcf(VS.80).aspx

http://msdn.microsoft.com/en-us/library/6t9t5wcf(VS.80).aspx

The Native Image Generator (Ngen.exe) is a tool that improves the performance of managed applications. Ngen.exe creates native images, which are files containing compiled processor-specific machine code, and installs them into the native image cache on the local computer. The runtime can use native images from the cache instead using the just-in-time (JIT) compiler to compile the original assembly.

本机映像生成器 (Ngen.exe) 是一种可提高托管应用程序性能的工具。Ngen.exe 创建本机映像,这些文件包含已编译的特定于处理器的机器代码,并将它们安装到本地计算机上的本机映像缓存中。运行时可以使用缓存中的本机图像,而不是使用即时 (JIT) 编译器来编译原始程序集。

回答by Mitchel Sellers

In addition to the coding best practices listed above including using StringBuilders when appropriate and items of that nature.

除了上面列出的编码最佳实践之外,还包括在适当的时候使用 StringBuilders 和这种性质的项目。

I highly recommend using a code profiling tool such as ANTs Profiler by RedGate. I have found that after taking the standard steps for optimization that using Profiler I can further optimize my code by quickly identifying the area(s) of code that are most heavily hit by the application.

我强烈建议使用代码分析工具,例如 RedGate 的 ANTs Profiler。我发现,在执行了使用 Profiler 的标准优化步骤后,我可以通过快速识别应用程序最严重的代码区域来进一步优化我的代码。