提示优化C#/。NET程序提示、程序、NET

2023-09-02 10:15:00 作者:倾此生只为伊人

好像优化是一项失传的艺术,这些天。是不存在的时候,所有的程序员挤每盎司效率从code?经常这样做而走5英里雪?

在带回一项失传的艺术精神,什么是你知道的简单(或者复杂)的变化,以优化C#/。NET code一些建议吗?因为它是如此广泛的事情,要看是什么人试图完成它会帮助提供上下文你的小费。例如:

当中将许多字符串连接在一起使用的StringBuilder 代替。见链接在底部的这一告诫。 使用的String.Compare 来比较做这样的事情 string1.ToLower()== string2.ToLower()

普遍的共识到目前为止,似乎测量是关键。这种遗漏的一点:测量不告诉你什么是错了,还是怎么办,如果你遇到了一个瓶颈。我跑进字符串连接瓶颈一次,不知道该怎么办,所以这些技巧是很有用的。

我的观点,即使发布,这是有共同的瓶颈的地方,以及他们如何甚至可以运行到它们之前是可以避免的。它甚至不是必要对即插即用code,任何人都应该盲目跟随,而是更多的获得一种理解,即表现应该至少在一定程度想过,而且有一些常见的陷阱看出来的。

我可以看到,虽然它可能是有用的还知道为什么一个尖端是有用的,它应该被应用。对于的StringBuilder 的提示,我发现我的帮助确实在不久前举行的在这里乔恩斯基特的网站。

解决方案   

好像优化是一项失传的艺术,这些天。

在制造,比如,显微镜被实践作为一门艺术,每天有一次。光学原则知之甚少。没有standarization份。管和齿轮和透镜不得不用手做,通过高度熟练的工人。

安装某一软件时,microsoft .net framework提示 应用程序的组件中发生了未经处理的异常,如果单击继续

这些天显微镜生产作为一门工程学科。物理学的基本原理是非常好理解的,现成的,货架配件被广泛使用,而显微镜建设的工程师们可以作出明智的选择,如何最好地优化他们的仪器它被设计成执行的任务。

这是业绩分析是一个失落的艺术是一个非常,非常好的事情。艺术是实践的作为一门艺术。优化要具体分析它是什么:一个的工程问题的解经仔细应用坚实的工程原则。

我一直问数十次多年来为我的技巧和窍门,人们可以用它来优化他们的VBScript /他们的JScript /他们的Active Server Pages /他们VB /他们的C#code列表。我一直抗拒这一点。 强调技巧和窍门是完全错误的方式来处理性能。这样导致code这是很难理解的,难推理,难以维护,这通常不是noticably比相应的简单的code更快。

正确的方式来处理性能接近它像任何其他问题,工程问题:

设置有意义的,可衡量的,以客户为中心的目标。 在构建测试套件来测试对现实的,但控制和可重复的条件下,这些目标的性能。 如果这些套房说明你没有达到自己的目标,使用的工具,如探查找出原因。 优化到底出什么探查确定为表现最差的子系统。请分析的每一个变化,让你清楚地了解每个对性能的影响。 重复,直到三种情况之一发生(1)你达到你的目标,并在销售软件,(2)您向下修正自己的目标的东西就可以实现,或者(3)你的项目被取消,因为你不能满足你进球。

这是一样的,你想解决什么其他的工程问题,比如增加一个功能 - 设定以客户为中心的目标为特征,在作出扎实落实跟踪进展情况,解决问题,因为你会发现他们通过精心调试分析,不断迭代,直到你船或失败。 效果是一种功能。

在复杂的现代系统的性能分析需要纪律和专注于扎实的工程原则,而不是在一个充满技巧,是狭义适用于琐碎或不切实际的情况袋。我从来没有一次通过应用程序的提示和技巧解决现实世界中的性能问题。

It seems like optimization is a lost art these days. Wasn't there a time when all programmers squeezed every ounce of efficiency from their code? Often doing so while walking five miles in the snow?

In the spirit of bringing back a lost art, what are some tips that you know of for simple (or perhaps complex) changes to optimize C#/.NET code? Since it's such a broad thing that depends on what one is trying to accomplish it'd help to provide context with your tip. For instance:

When concatenating many strings together use StringBuilder instead. See link at the bottom for caveats on this. Use string.Compare to compare two strings instead of doing something like string1.ToLower() == string2.ToLower()

The general consensus so far seems to be measuring is key. This kind of misses the point: measuring doesn't tell you what's wrong, or what to do about it if you run into a bottleneck. I ran into the string concatenation bottleneck once and had no idea what to do about it, so these tips are useful.

My point for even posting this is to have a place for common bottlenecks and how they can be avoided before even running into them. It's not even necessarily about plug and play code that anyone should blindly follow, but more about gaining an understanding that performance should be thought about, at least somewhat, and that there's some common pitfalls to look out for.

I can see though that it might be useful to also know why a tip is useful and where it should be applied. For the StringBuilder tip I found the help I did long ago at here on Jon Skeet's site.

解决方案

It seems like optimization is a lost art these days.

There was once a day when manufacture of, say, microscopes was practiced as an art. The optical principles were poorly understood. There was no standarization of parts. The tubes and gears and lenses had to be made by hand, by highly skilled workers.

These days microscopes are produced as an engineering discipline. The underlying principles of physics are extremely well understood, off-the-shelf parts are widely available, and microscope-building engineers can make informed choices as to how to best optimize their instrument to the tasks it is designed to perform.

That performance analysis is a "lost art" is a very, very good thing. That art was practiced as an art. Optimization should be approached for what it is: an engineering problem solvable through careful application of solid engineering principles.

I have been asked dozens of times over the years for my list of "tips and tricks" that people can use to optimize their vbscript / their jscript / their active server pages / their VB / their C# code. I always resist this. Emphasizing "tips and tricks" is exactly the wrong way to approach performance. That way leads to code which is hard to understand, hard to reason about, hard to maintain, that is typically not noticably faster than the corresponding straightforward code.

The right way to approach performance is to approach it as an engineering problem like any other problem:

Set meaningful, measurable, customer-focused goals. Build test suites to test your performance against these goals under realistic but controlled and repeatable conditions. If those suites show that you are not meeting your goals, use tools such as profilers to figure out why. Optimize the heck out of what the profiler identifies as the worst-performing subsystem. Keep profiling on every change so that you clearly understand the performance impact of each. Repeat until one of three things happens (1) you meet your goals and ship the software, (2) you revise your goals downwards to something you can achieve, or (3) your project is cancelled because you could not meet your goals.

This is the same as you'd solve any other engineering problem, like adding a feature -- set customer focused goals for the feature, track progress on making a solid implementation, fix problems as you find them through careful debugging analysis, keep iterating until you ship or fail. Performance is a feature.

Performance analysis on complex modern systems requires discipline and focus on solid engineering principles, not on a bag full of tricks that are narrowly applicable to trivial or unrealistic situations. I have never once solved a real-world performance problem through application of tips and tricks.