C# 什么是代码优化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/540305/
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-04 07:22:22  来源:igfitidea点击:

What is code optimization?

c#optimization

提问by Abdullah BaMusa

When said this code need some optimization, or can be some how optimized, what does that mean? which kind of code need optimization? How to apply optimization to the code in c#? What the benefits from that?

当说这段代码需要一些优化,或者可以如何优化时,这是什么意思?什么样的代码需要优化?如何对c#中的代码应用优化?这样做有什么好处?

采纳答案by Anton Gogolev

Optimizationis a very broad term. In general it implies modifying the system to make some of its aspect to work more efficiently or use fewer resources or be more robust. For example, a computer program may be optimized so that it will execute faster or use less memory or disk storage or be more responsive in terms of UI.

优化是一个非常广泛的术语。一般来说,它意味着修改系统以使其某些方面更有效地工作或使用更少的资源或更健壮。例如,可以优化计算机程序,使其执行得更快或使用更少的内存或磁盘存储,或者在 UI 方面更具响应性。

Although "optimization" has the same root as "optimal", the process of optimization does not produce a totally optimal system: there's always a trade-off, so only attributes of greatest interest are optimized.

尽管“优化”与“最优”具有相同的根源,但优化过程不会产生完全优化的系统:始终存在权衡,因此仅优化最感兴趣的属性。

And remember:

并记住:

The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet. (Michael A. Hymanson)

程序优化的第一条规则:不要这样做。程序优化的第二条规则(仅限专家!):先不要这样做。(迈克尔·A·Hyman逊

回答by tehvan

It might be for example that the code has a block of code which is duplicated, and could/should be put into a method, you might be using deprecated methods/classes, there might be simpler ways to do what the code is doing, there might be some cleaning up to do (e.g. remove hard coding) etc...

例如,代码可能有一个重复的代码块,并且可以/应该放入一个方法中,您可能正在使用不推荐使用的方法/类,可能有更简单的方法来执行代码正在执行的操作,有可能需要一些清理工作(例如删除硬编码)等...

回答by Steve Rowe

Code optimization is making code run faster. There are two primary ways of doing this:

代码优化使代码运行得更快。有两种主要方法可以做到这一点:

1) Squeezing more work into less cycles. Figure out where the code is doing an extra copy or if there is a branch in a tight loop. This is optimizing in the small.

1)将更多的工作压缩到更少的周期中。找出代码在何处进行额外复制,或者是否存在紧密循环中的分支。这是在小的优化。

2) Making your algorithms scale better. You may have heard of "Big O" notation. This is making an algorithm degrade much less quickly with large sets of data.

2)让你的算法更好地扩展。您可能听说过“大 O”符号。这使得算法在处理大量数据时降级的速度要慢得多。

For instance, if you naively search a phone book for a name you will start on page 1 and read all the names until you find the one you are looking for. This will take a number of instructions scaled by the number of names in the phone book. We call this O(n). Now think about how you really search the phone book. You open to some place toward the middle and see which side the name you are looking for is on. This is called a binary search and scales at the logarithm of the number of names. We call this O(logn). It's much faster.

例如,如果您天真地在电话簿中搜索某个姓名,您将从第 1 页开始阅读所有姓名,直到找到您要查找的姓​​名。这将需要根据电话簿中姓名数量缩放的许多指令。我们称之为 O(n)。现在想想你是如何真正搜索电话簿的。你打开中间的某个地方,看看你要找的名字在哪一边。这称为二分搜索,并按名称数量的对数进行缩放。我们称之为 O(logn)。它要快得多。

Remember the first rule of optimization: Measure first. Many man years have been spent optimizing code that wasn't run very much.

记住优化的第一条规则:先测量。许多人多年来一直在优化运行不多的代码。

回答by gimel

Optimizationis the process of modifying a system to make some aspect of it work more efficiently or use fewer resources.

优化是修改系统以使其某些方面更有效地工作或使用更少资源的过程。

In your case refers mainly to 2 levels:

在你的情况下主要是指2个级别:

Design level

At the highest level, the design may be optimized to make best use of the available resources. The implementation of this design will benefit from a good choice of efficient algorithms and the implementation of these algorithms will benefit from writing good quality code. The architectural design of a system overwhelmingly affects its performance. The choice of algorithm affects efficiency more than any other item of the design. In some cases, however, optimization relies on using fancier algorithms, making use of special cases and special tricks and performing complex trade-offs; thus, a fully optimized program can sometimes, if insufficiently commented, be more difficult for less experienced programmers to comprehend and hence may contain more faults than unoptimized versions.

Source code level

Avoiding bad quality coding can also improve performance, by avoiding obvious slowdowns. After that, however, some optimizations are possible which actually decrease maintainability; some, but not all of them can nowadays be performed by optimizing compilers. For instance, using more indirection is often needed to simplify or improve a software, but that indirection has a cost.

设计水平

在最高级别,可以优化设计以充分利用可用资源。这种设计的实现将受益于高效算法的良好选择,而这些算法的实现将受益于编写高质量的代码。系统的体系结构设计对其性能影响极大。算法的选择对效率的影响比设计的任何其他项目都大。然而,在某些情况下,优化依赖于使用更高级的算法,利用特殊情况和特殊技巧并进行复杂的权衡;因此,一个完全优化的程序有时如果注释不充分,对于经验不足的程序员来说更难理解,因此可能比未优化的版本包含更多的错误。

源代码级别

避免质量差的编码还可以通过避免明显的减速来提高性能。然而,在那之后,可能会进行一些优化,这实际上降低了可维护性;现在可以通过优化编译器来执行一些,但不是全部。例如,通常需要使用更多的间接来简化或改进软件,但这种间接是有代价的。

回答by SmacL

To add to Anton Gogolev's answer, when a piece of code needs optimisation, it is because a particular performance requirement is not met. We develop programs to meet users requirements, right? Most programmers tend to think largely in terms of functional requirements, i.e. what the program does, but users will also have performance requirements, what is the resource cost (network bandwidth, CPU cycles, memory, disk space, etc...) of providing the functionality. Optimization is the process of changing a piece of code to meet a specific performance requirement. IMHO this should happen at design time, but you will sometimes write a piece of code only to discover it underperforms. To optimize the code, you first have to find out which is the resource that you are over using. If it is CPU cycles or memory, a profiler might help. If it is network bandwidth, which is a very common one these days, you will need to do some load testing and comms profiling.

补充一下 Anton Gogolev 的回答,当一段代码需要优化时,是因为没有满足特定的性能要求。我们开发程序来满足用户的要求,对吗?大多数程序员倾向于在很大程度上考虑功能要求,即程序做什么,但用户也会有性能要求,提供什么资源成本(网络带宽、CPU 周期、内存、磁盘空间等...)功能。优化是更改一段代码以满足特定性能要求的过程。恕我直言,这应该在设计时发生,但您有时会编写一段代码,却发现它表现不佳。要优化代码,您首先必须找出您过度使用的资源。如果是 CPU 周期或内存,分析器可能会有所帮助。

My advice would be to always understand your current and probable future perfromance requirements before writing code, and optimize at design stage. Late optimization is expensive, difficult, and often either fails or results in ugly code.

我的建议是在编写代码之前始终了解您当前和可能的未来性能要求,并在设计阶段进行优化。后期优化既昂贵又困难,并且经常失败或导致代码丑陋。

回答by mouviciel

Optimization has two main purposes:

优化有两个主要目的:

  • getting your software use less resources, e.g., run faster, be smaller, use less RAM, less hard disk space both when running and when storing documents, less network access, ...

  • getting your software be more maintainable, by refactoring it.

  • 让您的软件使用更少的资源,例如,运行得更快、更小、使用更少的 RAM、在运行和存储文档时更少的硬盘空间、更少的网络访问,......

  • 通过重构,使您的软件更易于维护。

You don't need to optimize as long as no related issue has been raised: It is far more difficult to debug optimized code than to optimize correct code.

只要没有提出相关问题,您就不需要优化:调试优化的代码比优化正确的代码要困难得多。

回答by thinkbeforecoding

When doing code optimization, you take a metric on your code and try to make it more efficient. The metric usually refers to a scarce resource.

在进行代码优化时,您会对代码进行度量并尝试使其更高效。该指标通常是指稀缺资源。

Here are common metrics

以下是常用指标

  • Execution speed (usually the first that comes to mind when saying optimization)
  • Memory consumption
  • Executable size (on embedded systems it can be important)
  • Database access
  • Remote service access (Make it less chatty, caching..)
  • Simplicity, readability, maintainability of the code
  • 执行速度(通常是说优化时首先想到的)
  • 内存消耗
  • 可执行文件大小(在嵌入式系统上可能很重要)
  • 数据库访问
  • 远程服务访问(让它不那么健谈,缓存..)
  • 代码的简单性、可读性、可维护性

After optimization the code should give the same result.

优化后的代码应该给出相同的结果。

The problem is that you have to make choices. Execution speed often comes with more memory consuption...

问题是你必须做出选择。执行速度往往伴随着更多的内存消耗......

You should also alwas consider optimization globally. Having a gain of 10ms in a loop when you then spend 1000ms waiting for a web service is totaly useless.

您还应该始终考虑全局优化。当您花费 1000 毫秒等待 Web 服务时,在循环中获得 10 毫秒的增益是完全没有用的。