优化 C#/.NET 程序的技巧 [英] Tips for optimizing C#/.NET programs

查看:20
本文介绍了优化 C#/.NET 程序的技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如今,优化似乎是一门失传的艺术.不是有一段时间所有程序员都从他们的代码中榨取每一盎司的效率吗?经常在雪地里步行五英里时这样做?

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?

本着找回失落艺术的精神,您知道哪些简单(或复杂)更改以优化 C#/.NET 代码的技巧?由于这是一个如此广泛的事情,取决于人们试图完成的事情,因此为您的提示提供上下文会有所帮助.例如:

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:

  • 当将多个字符串连接在一起时,请改用 StringBuilder.有关这方面的注意事项,请参阅底部的链接.
  • 使用 string.Compare 来比较两个字符串,而不是像 string1.ToLower() == string2.ToLower()
  • 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.

我可以看到,了解提示为什么有用以及应该在哪里应用可能也很有用.对于 StringBuilder 提示,我在 here 找到了很久以前的帮助在 Jon Skeet 的网站上.

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.

多年来,我被问过几十次我的提示和技巧"列表,人们可以用它们来优化他们的 vbscript/他们的 jscript/他们的活动服务器页面/他们的 VB/他们的 C# 代码.我总是抗拒这一点.强调技巧和窍门"正是处理性能的错误方式.这种方式导致代码难以理解、难以推理、难以维护,通常不会明显快于对应的简单代码.

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:

  • 设定有意义的、可衡量的、以客户为中心的目标.
  • 构建测试套件,在现实但可控且可重复的条件下针对这些目标测试您的性能.
  • 如果这些套件表明您没有达到目标,请使用分析器等工具找出原因.
  • 优化分析器认为性能最差的子系统.不断分析每项更改,以便您清楚地了解每项更改对性能的影响.
  • 重复直到以下三件事之一发生 (1) 您实现了目标并发布了软件,(2) 您将目标向下修改为您可以实现的目标,或者 (3) 您的项目因无法满足您的要求而被取消目标.

这与您解决任何其他工程问题相同,例如添加功能——为该功能设定以客户为中心的目标、跟踪实现可靠实施的进度、通过仔细的调试分析发现问题时修复它们,不断迭代,直到你发货或失败.性能是一种特性.

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.

这篇关于优化 C#/.NET 程序的技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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